diff --git a/Cargo.toml b/Cargo.toml index c0a85b893..c1195777f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,6 +77,7 @@ time = "0.1" encoding = "0.2" language-tags = "0.2" lazy_static = "1.0" +parking_lot = "0.5" url = { version="1.7", features=["query_encoding"] } cookie = { version="0.10", features=["percent-encode"] } brotli2 = { version="^0.3.2", optional = true } diff --git a/src/fs.rs b/src/fs.rs index 9962f68b4..9d5659f72 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -4,9 +4,8 @@ use std::fs::{DirEntry, File, Metadata}; use std::io::{Read, Seek}; use std::ops::{Deref, DerefMut}; use std::path::{Path, PathBuf}; -use std::sync::Mutex; use std::time::{SystemTime, UNIX_EPOCH}; -use std::{cmp, env, io}; +use std::{cmp, io}; #[cfg(unix)] use std::os::unix::fs::MetadataExt; @@ -25,9 +24,7 @@ use httpmessage::HttpMessage; use httprequest::HttpRequest; use httpresponse::HttpResponse; use param::FromParam; - -/// Env variable for default cpu pool size for `StaticFiles` -const ENV_CPU_POOL_VAR: &str = "ACTIX_FS_POOL"; +use server::settings::DEFAULT_CPUPOOL; /// Return the MIME type associated with a filename extension (case-insensitive). /// If `ext` is empty or no associated type for the extension was found, returns @@ -572,32 +569,15 @@ pub struct StaticFiles { _follow_symlinks: bool, } -lazy_static! { - static ref DEFAULT_CPUPOOL: Mutex = { - let default = match env::var(ENV_CPU_POOL_VAR) { - Ok(val) => { - if let Ok(val) = val.parse() { - val - } else { - error!("Can not parse ACTIX_FS_POOL value"); - 20 - } - } - Err(_) => 20, - }; - Mutex::new(CpuPool::new(default)) - }; -} - impl StaticFiles { /// Create new `StaticFiles` instance for specified base directory. /// /// `StaticFile` uses `CpuPool` for blocking filesystem operations. /// By default pool with 20 threads is used. - /// Pool size can be changed by setting ACTIX_FS_POOL environment variable. + /// Pool size can be changed by setting ACTIX_CPU_POOL environment variable. pub fn new>(dir: T) -> StaticFiles { // use default CpuPool - let pool = { DEFAULT_CPUPOOL.lock().unwrap().clone() }; + let pool = { DEFAULT_CPUPOOL.lock().clone() }; StaticFiles::with_pool(dir, pool) } diff --git a/src/lib.rs b/src/lib.rs index 6e8cc2462..dc956ac99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,6 +112,7 @@ extern crate mime; extern crate mime_guess; extern crate mio; extern crate net2; +extern crate parking_lot; extern crate rand; extern crate slab; extern crate tokio; diff --git a/src/server/mod.rs b/src/server/mod.rs index 978454abd..82ba24e25 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -15,7 +15,7 @@ mod h1writer; mod h2; mod h2writer; pub(crate) mod helpers; -mod settings; +pub(crate) mod settings; pub(crate) mod shared; mod srv; pub(crate) mod utils; diff --git a/src/server/settings.rs b/src/server/settings.rs index c4037b339..7ea08c9d0 100644 --- a/src/server/settings.rs +++ b/src/server/settings.rs @@ -1,11 +1,12 @@ use std::cell::{Cell, RefCell, RefMut, UnsafeCell}; use std::fmt::Write; use std::rc::Rc; -use std::{fmt, mem, net}; +use std::{env, fmt, mem, net}; use bytes::BytesMut; -use futures_cpupool::{Builder, CpuPool}; +use futures_cpupool::CpuPool; use http::StatusCode; +use parking_lot::Mutex; use time; use super::channel::Node; @@ -15,6 +16,26 @@ use super::KeepAlive; use body::Body; use httpresponse::{HttpResponse, HttpResponseBuilder, HttpResponsePool}; +/// Env variable for default cpu pool size +const ENV_CPU_POOL_VAR: &str = "ACTIX_CPU_POOL"; + +lazy_static! { + pub(crate) static ref DEFAULT_CPUPOOL: Mutex = { + let default = match env::var(ENV_CPU_POOL_VAR) { + Ok(val) => { + if let Ok(val) = val.parse() { + val + } else { + error!("Can not parse ACTIX_CPU_POOL value"); + 20 + } + } + Err(_) => 20, + }; + Mutex::new(CpuPool::new(default)) + }; +} + /// Various server settings pub struct ServerSettings { addr: Option, @@ -106,7 +127,8 @@ impl ServerSettings { unsafe { let val = &mut *self.cpu_pool.get(); if val.is_none() { - *val = Some(Builder::new().pool_size(2).create()); + let pool = DEFAULT_CPUPOOL.lock().clone(); + *val = Some(pool); } val.as_ref().unwrap() }