From 2ca0ea70c406736d9612c5075f2feac5fefad4bc Mon Sep 17 00:00:00 2001
From: Nikolay Kim <fafhrd91@gmail.com>
Date: Thu, 12 Apr 2018 15:50:20 -0700
Subject: [PATCH] use one default cpu pool for StaticFiles #174

---
 CHANGES.md |  2 ++
 src/fs.rs  | 12 +++++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)

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<S> {
     _follow_symlinks: bool,
 }
 
+lazy_static!{
+    static ref DEFAULT_CPUPOOL: Mutex<CpuPool> = Mutex::new(CpuPool::new(20));
+}
+
 impl<S: 'static> StaticFiles<S> {
 
     /// Create new `StaticFiles` instance for specified base directory.
@@ -430,12 +435,17 @@ impl<S: 'static> StaticFiles<S> {
             }
         };
 
+        // 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,