diff --git a/actix-threadpool/src/lib.rs b/actix-threadpool/src/lib.rs index 56e5c528..65db87eb 100644 --- a/actix-threadpool/src/lib.rs +++ b/actix-threadpool/src/lib.rs @@ -1,15 +1,19 @@ //! Thread pool for blocking operations //! //! The pool would lazily generate thread according to the workload and spawn up to a total amount -//! of you machine's `logical CPU cores * 5` threads. Any spawned threads kept idle for 3 minutes +//! of you machine's `logical CPU cores * 5` threads. Any spawned threads kept idle for 5 minutes //! would be recycled and de spawned. //! +//! *. Settings are configuable through env variables. +//! //! # Example: //! ```rust //! #[actix_rt::main] //! async fn main() { //! // Optional: Set the max thread count for the blocking pool. //! std::env::set_var("ACTIX_THREADPOOL", "30"); +//! // Optional: Set the min thread count for the blocking pool. +//! std::env::set_var("ACTIX_THREADPOOL_MIN", "1"); //! // Optional: Set the timeout duration IN SECONDS for the blocking pool's idle threads. //! std::env::set_var("ACTIX_THREADPOOL_TIMEOUT", "300"); //! @@ -41,15 +45,18 @@ use derive_more::Display; use futures_channel::oneshot; use jian_rs::ThreadPool; -/// Env variable for default cpu pool size. +/// Env variable for default cpu pool max size. const ENV_MAX_THREADS: &str = "ACTIX_THREADPOOL"; +/// Env variable for default cpu pool min size. +const ENV_MIN_THREADS: &str = "ACTIX_THREADPOOL_MIN"; + /// Env variable for default thread idle timeout duration. const ENV_IDLE_TIMEOUT: &str = "ACTIX_THREADPOOL_TIMEOUT"; lazy_static::lazy_static! { pub(crate) static ref POOL: ThreadPool = { - let num = std::env::var(ENV_MAX_THREADS) + let max = std::env::var(ENV_MAX_THREADS) .map_err(|_| ()) .and_then(|val| { val.parse().map_err(|_| log::warn!( @@ -59,6 +66,16 @@ lazy_static::lazy_static! { }) .unwrap_or_else(|_| num_cpus::get() * 5); + let min = std::env::var(ENV_MIN_THREADS) + .map_err(|_| ()) + .and_then(|val| { + val.parse().map_err(|_| log::warn!( + "Can not parse {} value, using default", + ENV_MIN_THREADS, + )) + }) + .unwrap_or_else(|_| 1); + let dur = std::env::var(ENV_IDLE_TIMEOUT) .map_err(|_| ()) .and_then(|val| { @@ -71,8 +88,8 @@ lazy_static::lazy_static! { ThreadPool::builder() .thread_name("actix-threadpool") - .max_threads(num) - .min_threads(1) + .max_threads(max) + .min_threads(min) .idle_timeout(Duration::from_secs(dur)) .build() };