From 0ba58d98e25ea5d52a66db14bcab580bee46ea95 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 27 Apr 2024 16:41:24 +0200 Subject: [PATCH] limit open connections --- src/main.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main.rs b/src/main.rs index be0d086..f8925b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,11 +5,14 @@ use std::{ net::{TcpListener, TcpStream}, path::{Path, PathBuf}, thread, + time::Duration, }; mod http; use http::{Content, Method, Request, RequestRange, Response, Status}; +const MAX_CONNECTIONS: usize = 256; + fn main() { let args: Vec = env::args().collect(); @@ -36,6 +39,11 @@ fn main() { Err(err) => println!("Error with incoming stream: {err}"), } threads.retain(|j| !j.is_finished()); + while threads.len() >= MAX_CONNECTIONS { + threads.retain(|j| !j.is_finished()); + thread::sleep(Duration::from_millis(500)); + println!("Warning: maximum connections reached ({MAX_CONNECTIONS})") + } println!("{} connections open", threads.len()); } }