From c67e647b113cfbcfa221f20481b33fd12de8cc99 Mon Sep 17 00:00:00 2001 From: eternal-flame-AD Date: Wed, 16 Oct 2024 18:17:30 -0500 Subject: [PATCH] Minimize main logic Signed-off-by: eternal-flame-AD --- src/main.rs | 43 ++++++++----------------------------------- src/serve.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2d0bfc5..ff57df9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,42 +48,15 @@ async fn main() { ) .await; - let (jh, handle) = serve::serve( + serve::run( state.clone(), - &args.listen, - args.tls_cert.as_deref(), - args.tls_key.as_deref(), + serve::start( + state, + &args.listen, + args.tls_cert.as_deref(), + args.tls_key.as_deref(), + ) + .await, ) .await; - - let mut gc_ticker = tokio::time::interval(std::time::Duration::from_secs(300)); - gc_ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); - - let mut sigterm = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) - .expect("Failed to register SIGTERM handler"); - - tokio::spawn(async move { - loop { - gc_ticker.tick().await; - state - .client_pool_ref() - .gc(std::time::Duration::from_secs(120)); - } - }); - - tokio::select! { - res = jh => { - if let Err(e) = res { - log::error!("Server error: {}", e); - } - } - _ = tokio::signal::ctrl_c() => { - log::info!("Gracefully shutting down..."); - handle.graceful_shutdown(Some(std::time::Duration::from_secs(30))); - } - _ = sigterm.recv() => { - log::info!("Received SIGTERM, shutting down..."); - handle.graceful_shutdown(Some(std::time::Duration::from_secs(30))); - } - } } diff --git a/src/serve.rs b/src/serve.rs index de130c8..df0f059 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -24,7 +24,7 @@ pub const HAS_TLS_FEATURE: bool = cfg!(feature = "tls"); /// - `tls_key`: The path to the TLS key file /// /// Use [`tokio::select`] to listen on multiple addresses -pub async fn serve< +pub async fn start< S: HasAppState + Evaluator + Send + Sync + 'static, E: IntoResponse + Send + Sync + 'static, >( @@ -80,3 +80,44 @@ pub async fn serve< } } } + +/// Run the server until exit +pub async fn run< + S: HasAppState + Evaluator + Send + Sync + 'static, + E: IntoResponse + Send + Sync + 'static, +>( + state: S, + handle: (JoinHandle>, Handle), +) { + let (jh, handle) = handle; + let mut gc_ticker = tokio::time::interval(std::time::Duration::from_secs(300)); + gc_ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + + let mut sigterm = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) + .expect("Failed to register SIGTERM handler"); + + tokio::spawn(async move { + loop { + gc_ticker.tick().await; + state + .client_pool_ref() + .gc(std::time::Duration::from_secs(120)); + } + }); + + tokio::select! { + res = jh => { + if let Err(e) = res { + log::error!("Server error: {}", e); + } + } + _ = tokio::signal::ctrl_c() => { + log::info!("Gracefully shutting down..."); + handle.graceful_shutdown(Some(std::time::Duration::from_secs(30))); + } + _ = sigterm.recv() => { + log::info!("Received SIGTERM, shutting down..."); + handle.graceful_shutdown(Some(std::time::Duration::from_secs(30))); + } + } +}