diff --git a/CHANGES.md b/CHANGES.md index 07a655040..fd954fc90 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,8 @@ * Fix end-of-stream handling in parse_payload #173 +* Fix StaticFiles generate a lot of threads #174 + ## 0.5.0 (2018-04-10) diff --git a/src/fs.rs b/src/fs.rs index 495a0510f..0f6120b2a 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -6,6 +6,7 @@ use std::fs::{File, DirEntry, Metadata}; use std::path::{Path, PathBuf}; use std::ops::{Deref, DerefMut}; use std::time::{SystemTime, UNIX_EPOCH}; +use std::sync::Mutex; #[cfg(unix)] use std::os::unix::fs::MetadataExt; @@ -409,6 +410,10 @@ pub struct StaticFiles { _follow_symlinks: bool, } +lazy_static!{ + static ref DEFAULT_CPUPOOL: Mutex = Mutex::new(CpuPool::new(20)); +} + impl StaticFiles { /// Create new `StaticFiles` instance for specified base directory. @@ -430,12 +435,17 @@ impl StaticFiles { } }; + // use default CpuPool + let pool = { + DEFAULT_CPUPOOL.lock().unwrap().clone() + }; + StaticFiles { directory: dir, accessible: access, index: None, show_index: false, - cpu_pool: CpuPool::new(40), + cpu_pool: pool, default: Box::new(WrapHandler::new( |_| HttpResponse::new(StatusCode::NOT_FOUND))), _chunk_size: 0,