From a37cfb89a2a56cac19d932b3338d1d5bebdc654b Mon Sep 17 00:00:00 2001 From: Linnnus Date: Wed, 2 Oct 2024 08:13:24 +0200 Subject: Use max_idle_time when waiting for connections --- src/main.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 19fa3bf..fefbca7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,7 +73,20 @@ async fn main() -> Result<(), Box> { // We start a loop to continuously accept incoming connections loop { - let (stream, _) = listener.accept().await.expect("accepting new connection"); + let (stream, _) = if let Some(max_idle_time) = config.max_idle_time { + let accept_future = listener.accept(); + let timeout_future = tokio::time::timeout(max_idle_time, accept_future); + match timeout_future.await { + Ok(accept_result) => accept_result, + Err(_) => { + eprintln!("Timed out waiting for new connection. Exiting."); + process::exit(0); + }, + } + } else { + listener.accept().await + }.expect("accepting connection"); + let io = TokioIo::new(stream); let cfg = config.clone(); -- cgit v1.2.3