diff --git a/Cargo.lock b/Cargo.lock index 52ee212..775ae59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index a0b9936..4983e82 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [] \ No newline at end of file +http_rrs = [] diff --git a/src/ezhttp/handler.rs b/src/ezhttp/handler.rs index 9a5db73..4c8e8c3 100644 --- a/src/ezhttp/handler.rs +++ b/src/ezhttp/handler.rs @@ -39,6 +39,7 @@ pub async fn handler_connection( return; }, } + } #[macro_export] diff --git a/src/ezhttp/mod.rs b/src/ezhttp/mod.rs index 97e523d..8067f42 100644 --- a/src/ezhttp/mod.rs +++ b/src/ezhttp/mod.rs @@ -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,12 +87,14 @@ async fn start_server_with_threadpool( running: Arc, ) -> Result<(), Box> 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(); server_clone.on_start(&host_clone).await; @@ -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;