Skip to content

Commit

Permalink
fix: gracefully shutdown on CTRL+C and SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
internationalcrisis committed Oct 1, 2024
1 parent e57eaa0 commit 64c6b5a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use std::{
};
use time::Duration;

use tokio::signal::unix::{signal, SignalKind};

use crate::dbg_msg;

type BoxResponse = Pin<Box<dyn Future<Output = Result<Response<Body>, String>> + Send>>;
Expand Down Expand Up @@ -267,8 +269,13 @@ impl Server {

// Bind server to address specified above. Gracefully shut down if CTRL+C is pressed
let server = HyperServer::bind(address).serve(make_svc).with_graceful_shutdown(async {
// Wait for the CTRL+C signal
tokio::signal::ctrl_c().await.expect("Failed to install CTRL+C signal handler");
// Wait for the CTRL+C or SIGTERM signals
let mut signal_terminate = signal(SignalKind::terminate()).expect("Failed to install SIGTERM signal handler");

tokio::select! {
_ = signal_terminate.recv() => (),
_ = tokio::signal::ctrl_c() => ()
}
});

server.boxed()
Expand Down

0 comments on commit 64c6b5a

Please sign in to comment.