threadpool sync and async my head is made from tin cans

This commit is contained in:
MeexReay 2024-09-08 01:48:58 +03:00
parent 9aec967306
commit f92f3456de
4 changed files with 31 additions and 7 deletions

17
Cargo.lock generated
View File

@ -84,8 +84,10 @@ checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
name = "ezhttp"
version = "0.1.6"
dependencies = [
"lazy_static",
"rusty_pool",
"serde_json",
"threadpool",
"tokio",
"tokio-io-timeout",
"urlencoding",
@ -198,6 +200,12 @@ version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
version = "0.2.155"
@ -436,6 +444,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 = "tokio"
version = "1.40.0"

View File

@ -15,6 +15,8 @@ serde_json = "1.0.128"
tokio = { version = "1.40.0", features = ["full"] }
rusty_pool = "0.7.0"
tokio-io-timeout = "1.2.0"
threadpool = "1.8.1"
lazy_static = "1.5.0"
[features]
http_rrs = []

View File

@ -39,6 +39,7 @@ pub async fn handler_connection<S: HttpServer + Send + 'static + Sync>(
return;
},
}
}
#[macro_export]

View File

@ -8,9 +8,10 @@ use std::{
};
use tokio::io::AsyncReadExt;
use rusty_pool::ThreadPool;
use threadpool::ThreadPool;
use tokio::net::{TcpListener, TcpStream};
use tokio::runtime::Runtime;
use tokio::sync::OnceCell;
use tokio_io_timeout::TimeoutStream;
pub mod error;
@ -86,11 +87,13 @@ async fn start_server_with_threadpool<T>(
running: Arc<AtomicBool>,
) -> Result<(), Box<dyn Error>>
where
T: HttpServer + Send + 'static,
T: HttpServer + Send + 'static + Sync,
{
let threadpool = ThreadPool::new(threads, threads * 10, Duration::from_secs(60));
let threadpool = ThreadPool::new(threads);
let server = Arc::new(server);
let listener = TcpListener::bind(host).await?;
let handler = Arc::new(OnceCell::new_with(Some(handler)));
let host_clone = String::from(host).clone();
let server_clone = server.clone();
@ -105,7 +108,8 @@ where
let now_server = Arc::clone(&server);
threadpool.spawn_await((&handler)(now_server, sock));
let handler_clone = handler.clone();
threadpool.execute(move || {Runtime::new().unwrap().block_on((&handler_clone.get().unwrap())(now_server, sock))});
}
threadpool.join();
@ -141,7 +145,7 @@ where
let now_server = Arc::clone(&server);
Runtime::new().unwrap().spawn((&handler)(now_server, sock));
tokio::spawn((&handler)(now_server, sock));
}
server.on_close().await;