diff --git a/CHANGES.md b/CHANGES.md index 8c0741103..453b7b2f7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,8 @@ ## 0.5.2 (2018-04-xx) +* Allow to configure StaticFiles's CpuPool, via static method or env variable + * Add support for custom handling of Json extractor errors #181 * Fix StaticFiles does not support percent encoded paths #177 diff --git a/src/error.rs b/src/error.rs index 7158d7e7a..aafd9b4b7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -568,7 +568,7 @@ impl InternalError { } } - /// Create `InternalError` with predefined `HttpResponse` + /// Create `InternalError` with predefined `HttpResponse`. pub fn from_response(cause: T, response: HttpResponse) -> Self { InternalError { cause, diff --git a/src/fs.rs b/src/fs.rs index 5f734d008..e865a6dd3 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -6,7 +6,7 @@ use std::ops::{Deref, DerefMut}; use std::path::{Path, PathBuf}; use std::sync::Mutex; use std::time::{SystemTime, UNIX_EPOCH}; -use std::{cmp, io}; +use std::{cmp, env, io}; #[cfg(unix)] use std::os::unix::fs::MetadataExt; @@ -26,6 +26,9 @@ 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"; + /// A file with an associated name; responds with the Content-Type based on the /// file extension. #[derive(Debug)] @@ -445,12 +448,37 @@ pub struct StaticFiles { } lazy_static! { - static ref DEFAULT_CPUPOOL: Mutex = Mutex::new(CpuPool::new(20)); + 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. pub fn new>(dir: T) -> StaticFiles { + // use default CpuPool + let pool = { DEFAULT_CPUPOOL.lock().unwrap().clone() }; + + StaticFiles::with_pool(dir, pool) + } + + /// Create new `StaticFiles` instance for specified base directory and `CpuPool`. + pub fn with_pool>(dir: T, pool: CpuPool) -> StaticFiles { let dir = dir.into(); let (dir, access) = match dir.canonicalize() { @@ -468,9 +496,6 @@ impl StaticFiles { } }; - // use default CpuPool - let pool = { DEFAULT_CPUPOOL.lock().unwrap().clone() }; - StaticFiles { directory: dir, accessible: access,