Module salvo_extra::timeout
source · Available on crate feature
timeout
only.Expand description
Middleware for controlling requests timeout.
If the request does not complete within the specified timeout it will be aborted and a 503 Service Unavailable
response will be sent.
This middleware can be used to deal with slow network attacks.
§Example
use std::time::Duration;
use salvo_core::prelude::*;
use salvo_extra::timeout::Timeout;
#[handler]
async fn fast() -> &'static str {
"hello"
}
#[handler]
async fn slow() -> &'static str {
tokio::time::sleep(Duration::from_secs(6)).await;
"hello"
}
#[tokio::main]
async fn main() {
let router = Router::new()
.hoop(Timeout::new(Duration::from_secs(5)))
.push(Router::with_path("slow").get(slow))
.push(Router::with_path("fast").get(fast));
let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
Server::new(acceptor).serve(router).await;
}
Structs§
- Middleware for controlling request timeout.