diff --git a/examples/timeout/Cargo.toml b/examples/timeout/Cargo.toml new file mode 100644 index 000000000..d894aaa31 --- /dev/null +++ b/examples/timeout/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "example-timeout" +version.workspace = true +edition.workspace = true +publish.workspace = true + +[dependencies] +salvo = { workspace = true, features = ["timeout"] } +tokio = { workspace = true, features = ["macros"] } +tracing.workspace = true +tracing-subscriber.workspace = true diff --git a/examples/timeout/src/main.rs b/examples/timeout/src/main.rs new file mode 100644 index 000000000..0edf03c34 --- /dev/null +++ b/examples/timeout/src/main.rs @@ -0,0 +1,27 @@ +use std::time::Duration; + +use salvo::prelude::*; + +#[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() { + tracing_subscriber::fmt().init(); + + let acceptor = TcpListener::new("0.0.0.0:5800").bind().await; + + 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)); + + Server::new(acceptor).serve(router).await; +}