-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
318796b
commit 1c19558
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |