Skip to content

Commit

Permalink
add: graceful shutdown example
Browse files Browse the repository at this point in the history
  • Loading branch information
Hujian99 committed Jan 19, 2024
1 parent a6350e8 commit 4af352a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Empty file.
23 changes: 23 additions & 0 deletions examples/graceful-shutdown/src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
})
}

0 comments on commit 4af352a

Please sign in to comment.