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

84
Cargo.lock generated
View File

@ -2,31 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "crossbeam-deque"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]] [[package]]
name = "data-stream" name = "data-stream"
version = "0.3.0" version = "0.3.0"
@ -63,24 +38,30 @@ dependencies = [
"thiserror", "thiserror",
] ]
[[package]]
name = "either"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]] [[package]]
name = "header-parsing" name = "header-parsing"
version = "0.3.1" version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa272d549c15ab350762fde6dc312ab5c1d383febc0218c40ab1a94b5de60e00" checksum = "aa272d549c15ab350762fde6dc312ab5c1d383febc0218c40ab1a94b5de60e00"
[[package]]
name = "hermit-abi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.14" version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]]
name = "libc"
version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]] [[package]]
name = "maud" name = "maud"
version = "0.27.0" version = "0.27.0"
@ -103,6 +84,16 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "num_cpus"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi",
"libc",
]
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.3.1" version = "2.3.1"
@ -148,7 +139,7 @@ dependencies = [
"maud", "maud",
"percent-encoding", "percent-encoding",
"pukram2html", "pukram2html",
"rayon", "threadpool",
"vn-settings", "vn-settings",
] ]
@ -170,26 +161,6 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]] [[package]]
name = "simple-color" name = "simple-color"
version = "0.2.3" version = "0.2.3"
@ -230,6 +201,15 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "threadpool"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa"
dependencies = [
"num_cpus",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.14" version = "1.0.14"

View File

@ -8,6 +8,6 @@ percent-encoding = "2.3"
maud = "0.27.0" maud = "0.27.0"
pukram2html = "0.3.0" pukram2html = "0.3.0"
data-stream = "0.3.0" data-stream = "0.3.0"
rayon = "1.10.0"
dialogi = "0.3.1" dialogi = "0.3.1"
vn-settings = "0.1.0" vn-settings = "0.1.0"
threadpool = "1.8.1"

View File

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