Use rayon instead of threadpool
This commit is contained in:
parent
eb594413d9
commit
7ad5eff063
78
Cargo.lock
generated
78
Cargo.lock
generated
@ -2,6 +2,31 @@
|
||||
# It is not intended for manual editing.
|
||||
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]]
|
||||
name = "data-stream"
|
||||
version = "0.2.1"
|
||||
@ -29,10 +54,10 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.3.9"
|
||||
name = "either"
|
||||
version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
||||
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
@ -40,12 +65,6 @@ version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.169"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
|
||||
|
||||
[[package]]
|
||||
name = "maud"
|
||||
version = "0.25.0"
|
||||
@ -68,16 +87,6 @@ dependencies = [
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[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]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.1"
|
||||
@ -134,7 +143,7 @@ dependencies = [
|
||||
"maud",
|
||||
"percent-encoding",
|
||||
"pukram2html",
|
||||
"threadpool",
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -155,6 +164,26 @@ dependencies = [
|
||||
"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]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
@ -177,15 +206,6 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "threadpool"
|
||||
version = "1.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa"
|
||||
dependencies = [
|
||||
"num_cpus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.14"
|
||||
|
||||
@ -8,4 +8,4 @@ percent-encoding = "2.3"
|
||||
maud = "0.25.0"
|
||||
pukram2html = "0.3.0"
|
||||
data-stream = "0.2.0"
|
||||
threadpool = "1.8.1"
|
||||
rayon = "1.10.0"
|
||||
|
||||
35
src/main.rs
35
src/main.rs
@ -6,8 +6,6 @@ use std::{
|
||||
net::{TcpListener, TcpStream},
|
||||
path::{Path, PathBuf},
|
||||
sync::{Arc, Mutex},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use data_stream::{
|
||||
@ -17,7 +15,7 @@ use data_stream::{
|
||||
use maud::html;
|
||||
use percent_encoding::percent_decode_str;
|
||||
use pukram2html::{Settings, convert, convert_extended, convert_subheader};
|
||||
use threadpool::ThreadPool;
|
||||
use rayon::prelude::*;
|
||||
|
||||
mod request;
|
||||
use request::Request;
|
||||
@ -46,37 +44,28 @@ fn start_server(
|
||||
eprintln!("Strated server!");
|
||||
|
||||
let context: Arc<Mutex<Context>> = Arc::default();
|
||||
let mut pool = ThreadPool::new(4);
|
||||
|
||||
for stream in listener.incoming() {
|
||||
listener.incoming().par_bridge().for_each(|stream| {
|
||||
eprintln!("New connection!");
|
||||
|
||||
let Ok(stream) = stream else {
|
||||
eprintln!("Connection failed!");
|
||||
continue;
|
||||
return;
|
||||
};
|
||||
|
||||
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();
|
||||
pool.execute(move || {
|
||||
handle_connection(
|
||||
context,
|
||||
path,
|
||||
stream,
|
||||
password.as_deref(),
|
||||
hidden_password.as_deref(),
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
handle_connection(
|
||||
context,
|
||||
path,
|
||||
stream,
|
||||
password.as_deref(),
|
||||
hidden_password.as_deref(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn fail(mut stream: TcpStream) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user