rama_http/layer/timeout/
mod.rs

1//! Middleware that applies a timeout to requests.
2//!
3//! If the request does not complete within the specified timeout it will be aborted and a `408
4//! Request Timeout` response will be sent.
5//!
6//! # Differences from `rama_core::service::layer::Timeout`
7//!
8//! The generic [`Timeout`] middleware uses an error to signal timeout, i.e.
9//! it changes the error type to [`BoxError`](rama_core::error::BoxError). For HTTP services that is rarely
10//! what you want as returning errors will terminate the connection without sending a response.
11//!
12//! This middleware won't change the error type and instead return a `408 Request Timeout`
13//! response. That means if your service's error type is [`Infallible`] it will still be
14//! [`Infallible`] after applying this middleware.
15//!
16//! # Example
17//!
18//! ```
19//! use std::{convert::Infallible, time::Duration};
20//!
21//! use rama_core::Layer;
22//! use rama_core::service::service_fn;
23//! use rama_http::{Body, Request, Response};
24//! use rama_http::layer::timeout::TimeoutLayer;
25//! use rama_core::error::BoxError;
26//!
27//! async fn handle(_: Request) -> Result<Response, Infallible> {
28//!     // ...
29//!     # Ok(Response::new(Body::empty()))
30//! }
31//!
32//! # #[tokio::main]
33//! # async fn main() -> Result<(), BoxError> {
34//! let svc = (
35//!     // Timeout requests after 30 seconds
36//!     TimeoutLayer::new(Duration::from_secs(30)),
37//! ).into_layer(service_fn(handle));
38//! # Ok(())
39//! # }
40//! ```
41//!
42//! [`Infallible`]: std::convert::Infallible
43
44mod body;
45mod service;
46
47pub use body::{TimeoutBody, TimeoutError};
48pub use service::{
49    RequestBodyTimeout, RequestBodyTimeoutLayer, ResponseBodyTimeout, ResponseBodyTimeoutLayer,
50    Timeout, TimeoutLayer,
51};