From 4af352a916d4e43447df6546677410ba788eecc1 Mon Sep 17 00:00:00 2001 From: zhufujian <835952293@qq.com> Date: Fri, 19 Jan 2024 11:12:57 +0800 Subject: [PATCH] add: graceful shutdown example --- examples/graceful-shutdown/Cargo.toml | 0 examples/graceful-shutdown/src/main.rs | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 examples/graceful-shutdown/Cargo.toml create mode 100644 examples/graceful-shutdown/src/main.rs diff --git a/examples/graceful-shutdown/Cargo.toml b/examples/graceful-shutdown/Cargo.toml new file mode 100644 index 000000000..e69de29bb diff --git a/examples/graceful-shutdown/src/main.rs b/examples/graceful-shutdown/src/main.rs new file mode 100644 index 000000000..5e94f5974 --- /dev/null +++ b/examples/graceful-shutdown/src/main.rs @@ -0,0 +1,23 @@ +use salvo_core::prelude::*; +use tokio::signal; + +#[tokio::main] +async fn main() { + let acceptor = TcpListener::new("127.0.0.1:5800").bind().await; + let server = Server::new(acceptor); + let handle = server.handle(); + + // Listen Shutdown Signal + listen_shutdown_signal(handle); + + server.serve(Router::new()).await; +} + +async fn listen_shutdown_signal(handle: ServerHandle) { + // Wait Shutdown Signal + tokio::spawn(async move { + let _ = signal::ctrl_c().await; + // Graceful Shutdown Server + handle.stop_graceful(None); + }) +}