Use threadpool instead of threads directly

This commit is contained in:
p11 2023-07-21 20:38:29 +02:00
parent 1b039b0494
commit 18cc1c9db6
3 changed files with 39 additions and 4 deletions

32
Cargo.lock generated
View File

@ -13,12 +13,24 @@ name = "formatting"
version = "0.1.0"
source = "git+https://gitlab.com/porky11/formatting#7edec9731c042bd2cdd7f3a1674cb2e1b0303e42"
[[package]]
name = "hermit-abi"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b"
[[package]]
name = "itoa"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b02a5381cc465bd3041d84623d0fa3b66738b52b8e2fc3bab8ad63ab032f4a"
[[package]]
name = "libc"
version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "maud"
version = "0.25.0"
@ -41,6 +53,16 @@ dependencies = [
"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]]
name = "percent-encoding"
version = "2.3.0"
@ -88,6 +110,7 @@ dependencies = [
"maud",
"percent-encoding",
"pukram2html",
"threadpool",
]
[[package]]
@ -118,6 +141,15 @@ 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.11"

View File

@ -8,3 +8,4 @@ percent-encoding = "2.3"
maud = "0.25.0"
pukram2html = { git = "https://gitlab.com/porky11/pukram2html" }
data-stream = "0.2.0"
threadpool = "1.8.1"

View File

@ -6,7 +6,6 @@ use std::{
net::{TcpListener, TcpStream},
path::Path,
sync::{Arc, Mutex},
thread,
};
use data_stream::{
@ -16,12 +15,15 @@ use data_stream::{
use maud::html;
use percent_encoding::percent_decode_str;
use pukram2html::{convert, convert_extended, convert_subheader, Settings};
use threadpool::ThreadPool;
mod request;
use request::Request;
fn main() {
let mut context = Context::default();
let pool = ThreadPool::new(4);
let mut args = env::args();
args.next();
let address = args.next().unwrap_or("127.0.0.1:8080".to_string());
@ -36,7 +38,7 @@ fn main() {
continue;
};
context.handle_connection(stream);
context.handle_connection(stream, &pool);
}
}
@ -118,7 +120,7 @@ struct Context {
}
impl Context {
fn handle_connection(&mut self, mut stream: TcpStream) {
fn handle_connection(&mut self, mut stream: TcpStream, pool: &ThreadPool) {
let Some(request) = Request::from(&stream) else {
eprintln!("Invalid request!");
return;
@ -207,7 +209,7 @@ impl Context {
.clone(),
};
thread::spawn(move || {
pool.execute(move || {
handle_relative_connection(
info,
stream,