Expand description
Per-route request timeouts.
A single global read timeout (30 s per connection, or RWS_CONFIG_*)
applies uniformly to every route today. A file-upload endpoint may
legitimately need 120 s while a health check must complete in 500 ms —
there’s no way to express that difference without wrapping a handler
yourself. This module provides that wrapping.
§Honest limitation: Rust cannot preempt a running synchronous call
For synchronous handlers (Router, AppWithState, plain Applications),
there is no safe way to forcibly stop a thread that’s already running.
with_timeout, with_timeout_state, and TimeoutLayer all run the
wrapped work on a background thread and bound how long they wait for
it — if the deadline passes, the caller gets a 504 Gateway Timeout
response immediately, but the background thread keeps running to
completion (its result is simply discarded). This bounds the client’s
wait time, not the handler’s actual resource usage.
For genuine cancellation, use with_timeout_async with
AsyncAppWithState (requires the http2 feature): dropping a Future
that hasn’t finished actually stops its execution at the next .await
point, backed by tokio::time::timeout.
§Example — Router
use rust_web_server::router::Router;
use rust_web_server::timeout::with_timeout;
use rust_web_server::response::Response;
use rust_web_server::core::New;
use std::time::Duration;
let router = Router::new()
.get("/healthz", with_timeout(Duration::from_millis(500), |_req, _params, _conn| Response::new()))
.post("/upload", with_timeout(Duration::from_secs(120), |_req, _params, _conn| Response::new()));§Example — wrapping a whole Application
use rust_web_server::app::App;
use rust_web_server::core::New;
use rust_web_server::timeout::TimeoutLayer;
use std::time::Duration;
let app = TimeoutLayer::new(App::new(), Duration::from_secs(5));Structs§
- Timeout
Layer - Wraps any owned
Application(or a sharedArc<dyn Application>) so every request through it must complete withindurationor the client gets504 Gateway Timeoutinstead of waiting further.
Functions§
- with_
timeout - Wraps a stateless handler (the
Routerhandler signature) so it must complete withindurationor the caller gets504 Gateway Timeoutinstead of waiting further. See the module docs for the cancellation caveat. - with_
timeout_ async - Wraps an
AsyncAppWithState<S>handler so its future is dropped — genuinely cancelled at its next.awaitpoint — if it doesn’t resolve withinduration. Backed bytokio::time::timeout; requires thehttp2feature. NoClonebound onS:AsyncAppWithStatealready passes state as an ownedArc<S>. - with_
timeout_ state - Wraps an
AppWithState<S>handler (which additionally receives&S) so it must complete withindurationor the caller gets504 Gateway Timeoutinstead of waiting further.