Skip to content

Commit

Permalink
Add timeout example
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Jun 2, 2024
1 parent 318796b commit 1c19558
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/timeout/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions examples/timeout/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 1c19558

Please sign in to comment.