Available on crate feature metrics only.
Expand description

Measure the number of in-flight requests.

In-flight requests is the number of requests a service is currently processing. The processing of a request starts when it is received by the service (tower::Service::call is called) and is considered complete when the response body is consumed, dropped, or an error happens.

Example

use tower::{Service, ServiceExt, ServiceBuilder};
use tower_http::metrics::InFlightRequestsLayer;
use http::{Request, Response};
use hyper::Body;
use std::{time::Duration, convert::Infallible};

async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
    // ...
}

async fn update_in_flight_requests_metric(count: usize) {
    // ...
}

// Create a `Layer` with an associated counter.
let (in_flight_requests_layer, counter) = InFlightRequestsLayer::pair();

// Spawn a task that will receive the number of in-flight requests every 10 seconds.
tokio::spawn(
    counter.run_emitter(Duration::from_secs(10), |count| async move {
        update_in_flight_requests_metric(count).await;
    }),
);

let mut service = ServiceBuilder::new()
    // Keep track of the number of in-flight requests. This will increment and decrement
    // `counter` automatically.
    .layer(in_flight_requests_layer)
    .service_fn(handle);

// Call the service.
let response = service
    .ready()
    .await?
    .call(Request::new(Body::empty()))
    .await?;

Structs