on-early-drop only.Expand description
Middleware that detects when a response future or response body is dropped before completion.
HTTP services typically learn nothing when a client disconnects mid-request. This middleware installs drop guards so that premature termination is observable: logs, metrics, cleanup.
Two events are distinguished:
- Future drop: the response future was dropped before the inner service produced any response.
- Body drop: the response body was dropped before reaching
is_end_stream.
§Example: bridge to trace::OnFailure
With the trace feature, EarlyDropsAsFailures wraps any
OnFailure<DroppedFailure> and routes both
events through it. Place this layer inside TraceLayer
so that the emitted events inherit the request span.
use tower_http::on_early_drop::{OnEarlyDropLayer, EarlyDropsAsFailures};
use tower_http::trace::DefaultOnFailure;
let layer = OnEarlyDropLayer::new(
EarlyDropsAsFailures::new(DefaultOnFailure::default()),
);Use OnEarlyDropLayer::builder to place EarlyDropsAsFailures in a
single slot (track only body or only future drops) or to install plain
closures.
§Example: builder with direct callbacks
Use OnEarlyDropLayer::builder to install closures in either or both
slots. The future-drop closure is a factory: outer closure runs at
request time, inner closure fires on drop. The body-drop closure uses a
three-level chain via OnBodyDropFn: outer at request time, middle at
response-ready time, inner on drop.
use http::Request;
use tower_http::on_early_drop::{OnBodyDropFn, OnEarlyDropLayer};
let layer = OnEarlyDropLayer::builder()
.on_future_drop(|req: &Request<()>| {
let uri = req.uri().clone();
move || eprintln!("future dropped for {}", uri)
})
.on_body_drop(OnBodyDropFn::new(|req: &Request<()>| {
let uri = req.uri().clone();
move |parts: &http::response::Parts| {
let status = parts.status;
move || eprintln!("body dropped for {} status {}", uri, status)
}
}));Chain just one of the two methods to hook only that event; the other slot stays no-op.
§Panics in callbacks
Callbacks fire from Drop. Panicking during a drop that occurs while
another panic is unwinding aborts the process. Closures and custom
OnDropCallback implementations must not panic.
§Standalone guard
OnEarlyDropGuard is usable on its own to detect early drop of
arbitrary scopes.
use tower_http::on_early_drop::OnEarlyDropGuard;
let mut guard = OnEarlyDropGuard::new(|| {
eprintln!("scope exited early");
});
// ... work that might return early ...
guard.completed();Structs§
- Body
Drop Failure Callback trace - Body-drop callback produced by
EarlyDropsAsFailures. - Body
Dropped - Context for
DroppedFailure::Body. - Early
Drops AsFailures trace - Bridges early-drop events to
trace::OnFailure. - Future
Drop Failure Callback trace - Future-drop callback produced by
EarlyDropsAsFailures. - Future
Dropped - Context for
DroppedFailure::Future. - Noop
Drop Callback - No-op callback used as the default for empty hook slots.
- OnBody
Drop Fn - Adapter making
FnMut(&Request) -> FnOnce(&Parts) -> FnOnce()closure chains implementOnBodyDrop. - OnEarly
Drop Body - Response body for
OnEarlyDropService. Fires its callback if dropped before reaching end-of-stream. - OnEarly
Drop Future - Response future for
OnEarlyDropService. - OnEarly
Drop Guard - Runs a callback on drop unless
completedis called first. - OnEarly
Drop Layer Layerthat appliesOnEarlyDropService.- OnEarly
Drop Service Serviceproduced byOnEarlyDropLayer.- PreResponse
Body Drop Callback trace - Intermediate produced by
OnBodyDrop::make_at_callforEarlyDropsAsFailures, carrying state forward toOnBodyDrop::make_at_response.
Enums§
- Dropped
Failure - Classification for early-drop events reported through
EarlyDropsAsFailures.
Traits§
- OnBody
Drop - Hook fired when the response body is dropped before reaching end-of-stream.
- OnDrop
Callback - Callback fired exactly once when an early-drop event is observed.
- OnFuture
Drop - Hook fired when the response future is dropped before producing a result.