gotham/middleware/
timer.rs

1//! Request timing middleware, used to measure response times of requests.
2use crate::handler::HandlerFuture;
3use crate::helpers::http::header::X_RUNTIME_DURATION;
4use crate::helpers::timing::Timer;
5use crate::middleware::{Middleware, NewMiddleware};
6use crate::state::State;
7use futures_util::future::{self, FutureExt, TryFutureExt};
8use std::pin::Pin;
9
10/// Middleware binding to attach request execution times inside headers.
11///
12/// This can be used to easily measure request time from outside the
13/// application, via the `x-runtime-duration` header in the response.
14#[derive(Clone)]
15pub struct RequestTimer;
16
17/// `Middleware` trait implementation.
18impl Middleware for RequestTimer {
19    /// Attaches the request execution time to the response headers.
20    fn call<Chain>(self, state: State, chain: Chain) -> Pin<Box<HandlerFuture>>
21    where
22        Chain: FnOnce(State) -> Pin<Box<HandlerFuture>>,
23    {
24        // start the timer
25        let timer = Timer::new();
26
27        // execute the chain and attach the time on complete
28        let f = chain(state).and_then(move |(state, mut response)| {
29            // attach the formatted header
30            response.headers_mut().insert(
31                X_RUNTIME_DURATION,
32                timer.elapsed().to_string().parse().unwrap(),
33            );
34
35            future::ok((state, response))
36        });
37
38        f.boxed()
39    }
40}
41
42/// `NewMiddleware` trait implementation.
43impl NewMiddleware for RequestTimer {
44    type Instance = Self;
45
46    /// Clones the current middleware to a new instance.
47    fn new_middleware(&self) -> anyhow::Result<Self::Instance> {
48        Ok(self.clone())
49    }
50}