limit open connections

This commit is contained in:
Crispy 2024-04-27 16:41:24 +02:00
parent 9170e7f7f8
commit 0ba58d98e2

View file

@ -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<String> = 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());
}
}