Use rayon instead of threadpool
This commit is contained in:
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) {
|
||||
|
||||
Reference in New Issue
Block a user