Use custom threadpool implementation again to avoid getting stuck

This commit is contained in:
p11
2025-04-09 22:47:10 +02:00
parent 819e55ff9c
commit b3d692ac7f
3 changed files with 56 additions and 65 deletions

View File

@@ -9,6 +9,8 @@ use std::{
Arc, Mutex,
atomic::{AtomicUsize, Ordering},
},
thread,
time::Duration,
};
use data_stream::{
@@ -19,7 +21,7 @@ use dialog::parse_map;
use maud::html;
use percent_encoding::percent_decode_str;
use pukram2html::{Settings, convert, convert_extended, convert_subheader};
use rayon::prelude::*;
use threadpool::ThreadPool;
mod request;
use request::Request;
@@ -54,28 +56,37 @@ fn start_server(
eprintln!("Strated server!");
let context: Arc<Mutex<Context>> = Arc::default();
let mut pool = ThreadPool::new(4);
listener.incoming().par_bridge().for_each(|stream| {
for stream in listener.incoming() {
eprintln!("New connection!");
let Ok(stream) = stream else {
eprintln!("Connection failed!");
return;
continue;
};
if pool.active_count() == pool.max_count() {
thread::sleep(Duration::from_secs(1));
if pool.active_count() == pool.max_count() {
pool = ThreadPool::new(pool.max_count())
}
}
let context = context.clone();
let path = path.clone();
let password = password.clone();
let hidden_password = partial_password.clone();
handle_connection(
context,
path,
stream,
password.as_deref(),
hidden_password.as_deref(),
)
})
pool.execute(move || {
handle_connection(
context,
path,
stream,
password.as_deref(),
hidden_password.as_deref(),
)
});
}
}
fn fail(mut stream: TcpStream) {