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 bytes::Bytes;
use http_body_util::Full;
use std::{time::Duration, convert::Infallible};
async fn handle(req: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, 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(Full::default()))
.await?;
Structs§
- Middleware that counts the number of in-flight requests.
- An atomic counter that keeps track of the number of in-flight requests.
- Layer for applying
InFlightRequests
which counts the number of in-flight requests. - Response body for
InFlightRequests
. - Response future for
InFlightRequests
.