diff options
author | Linnnus <[email protected]> | 2024-10-02 08:13:24 +0200 |
---|---|---|
committer | Linnnus <[email protected]> | 2024-10-02 08:13:24 +0200 |
commit | a37cfb89a2a56cac19d932b3338d1d5bebdc654b (patch) | |
tree | 0612ef1a3d24de16b8a843f45595bf1fc4f8c8b8 | |
parent | 73e03db9e696a6beccb7307eaab7af71e2dd936c (diff) |
Use max_idle_time when waiting for connections
-rw-r--r-- | src/main.rs | 15 |
1 files changed, 14 insertions, 1 deletions
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<dyn std::error::Error + Send + Sync>> { // 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(); |