From 9002c8a0eb7fa4400e6c2be520fea9458fbb25fd Mon Sep 17 00:00:00 2001 From: internationalcrisis Date: Tue, 1 Oct 2024 03:59:48 -0500 Subject: [PATCH] fix: gracefully shutdown on CTRL+C and SIGTERM Fixes #205 --- src/server.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/server.rs b/src/server.rs index f69d2007..96b1a83b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -26,6 +26,9 @@ use std::{ }; use time::Duration; +#[cfg(unix)] +use tokio::signal::unix::{signal, SignalKind}; + use crate::dbg_msg; type BoxResponse = Pin, String>> + Send>>; @@ -267,8 +270,17 @@ 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 { + #[cfg(windows)] // Wait for the CTRL+C signal tokio::signal::ctrl_c().await.expect("Failed to install CTRL+C signal handler"); + + #[cfg(unix)] + // Wait for CTRL+C or SIGTERM signals + let mut signal_terminate = signal(SignalKind::terminate()).expect("Failed to install SIGTERM signal handler"); + tokio::select! { + _ = tokio::signal::ctrl_c() => (), + _ = signal_terminate.recv() => () + } }); server.boxed()