From 0d58e14876ed8e4b2b4fc10b3ef67cd703dee1a1 Mon Sep 17 00:00:00 2001 From: Vika Date: Fri, 15 Apr 2022 07:42:39 +0300 Subject: feat: graceful shutdown Now Kittybox can gracefully shutdown on SIGTERM. Nice! TODO: consider shutting down on multiple signals --- src/main.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 866fcf3..8e610e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use log::{debug, error, info}; use std::{convert::Infallible, env, time::Duration}; -use http_types::Url; +use url::Url; use hyper::client::{HttpConnector,connect::dns::GaiResolver}; use hyper_rustls::HttpsConnector; use warp::{Filter, host::Authority}; @@ -212,7 +212,7 @@ async fn main() { tcp_listener.set_nonblocking(true).unwrap(); info!("Listening on {}", tcp_listener.local_addr().unwrap()); - let server: hyper::server::Server<_, _> = hyper::server::Server::from_tcp(tcp_listener) + let server = hyper::server::Server::from_tcp(tcp_listener) .unwrap() .tcp_keepalive(Some(Duration::from_secs(30 * 60))) .serve(hyper::service::make_service_fn(move |_| { @@ -220,8 +220,24 @@ async fn main() { async move { Ok::<_, Infallible>(service) } - })); - + })) + .with_graceful_shutdown(async move { + // Defer to C-c handler whenever we're not on Unix + // TODO consider using a diverging future here + #[cfg(not(unix))] + return tokio::signal::ctrl_c().await.unwrap(); + #[cfg(unix)] + { + use tokio::signal::unix::{signal, SignalKind}; + + signal(SignalKind::terminate()) + .unwrap() + .recv() + .await + .unwrap() + } + }); + if let Err(err) = server.await { error!("Error serving requests: {}", err); std::process::exit(1); -- cgit 1.4.1