add env var for min_thread count.

This commit is contained in:
fakeshadow 2020-07-23 20:19:38 +08:00
parent 318e2714f3
commit bcb3ec40f5
1 changed files with 22 additions and 5 deletions

View File

@ -1,15 +1,19 @@
//! Thread pool for blocking operations //! Thread pool for blocking operations
//! //!
//! The pool would lazily generate thread according to the workload and spawn up to a total amount //! 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. //! would be recycled and de spawned.
//! //!
//! *. Settings are configuable through env variables.
//!
//! # Example: //! # Example:
//! ```rust //! ```rust
//! #[actix_rt::main] //! #[actix_rt::main]
//! async fn main() { //! async fn main() {
//! // Optional: Set the max thread count for the blocking pool. //! // Optional: Set the max thread count for the blocking pool.
//! std::env::set_var("ACTIX_THREADPOOL", "30"); //! 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. //! // Optional: Set the timeout duration IN SECONDS for the blocking pool's idle threads.
//! std::env::set_var("ACTIX_THREADPOOL_TIMEOUT", "300"); //! std::env::set_var("ACTIX_THREADPOOL_TIMEOUT", "300");
//! //!
@ -41,15 +45,18 @@ use derive_more::Display;
use futures_channel::oneshot; use futures_channel::oneshot;
use jian_rs::ThreadPool; 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"; 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. /// Env variable for default thread idle timeout duration.
const ENV_IDLE_TIMEOUT: &str = "ACTIX_THREADPOOL_TIMEOUT"; const ENV_IDLE_TIMEOUT: &str = "ACTIX_THREADPOOL_TIMEOUT";
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub(crate) static ref POOL: ThreadPool = { pub(crate) static ref POOL: ThreadPool = {
let num = std::env::var(ENV_MAX_THREADS) let max = std::env::var(ENV_MAX_THREADS)
.map_err(|_| ()) .map_err(|_| ())
.and_then(|val| { .and_then(|val| {
val.parse().map_err(|_| log::warn!( val.parse().map_err(|_| log::warn!(
@ -59,6 +66,16 @@ lazy_static::lazy_static! {
}) })
.unwrap_or_else(|_| num_cpus::get() * 5); .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) let dur = std::env::var(ENV_IDLE_TIMEOUT)
.map_err(|_| ()) .map_err(|_| ())
.and_then(|val| { .and_then(|val| {
@ -71,8 +88,8 @@ lazy_static::lazy_static! {
ThreadPool::builder() ThreadPool::builder()
.thread_name("actix-threadpool") .thread_name("actix-threadpool")
.max_threads(num) .max_threads(max)
.min_threads(1) .min_threads(min)
.idle_timeout(Duration::from_secs(dur)) .idle_timeout(Duration::from_secs(dur))
.build() .build()
}; };