1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00

Allow to configure StaticFiles CpuPool, via static method or env variable

This commit is contained in:
Nikolay Kim 2018-04-13 19:46:14 -07:00
parent 634c5723a0
commit a9ea649348
3 changed files with 33 additions and 6 deletions

View File

@ -3,6 +3,8 @@
## 0.5.2 (2018-04-xx) ## 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 * Add support for custom handling of Json extractor errors #181
* Fix StaticFiles does not support percent encoded paths #177 * Fix StaticFiles does not support percent encoded paths #177

View File

@ -568,7 +568,7 @@ impl<T> InternalError<T> {
} }
} }
/// Create `InternalError` with predefined `HttpResponse` /// Create `InternalError` with predefined `HttpResponse`.
pub fn from_response(cause: T, response: HttpResponse) -> Self { pub fn from_response(cause: T, response: HttpResponse) -> Self {
InternalError { InternalError {
cause, cause,

View File

@ -6,7 +6,7 @@ use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::Mutex; use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use std::{cmp, io}; use std::{cmp, env, io};
#[cfg(unix)] #[cfg(unix)]
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
@ -26,6 +26,9 @@ use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
use param::FromParam; 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 /// A file with an associated name; responds with the Content-Type based on the
/// file extension. /// file extension.
#[derive(Debug)] #[derive(Debug)]
@ -445,12 +448,37 @@ pub struct StaticFiles<S> {
} }
lazy_static! { lazy_static! {
static ref DEFAULT_CPUPOOL: Mutex<CpuPool> = Mutex::new(CpuPool::new(20)); static ref DEFAULT_CPUPOOL: Mutex<CpuPool> = {
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<S: 'static> StaticFiles<S> { impl<S: 'static> StaticFiles<S> {
/// Create new `StaticFiles` instance for specified base directory. /// 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<T: Into<PathBuf>>(dir: T) -> StaticFiles<S> { pub fn new<T: Into<PathBuf>>(dir: T) -> StaticFiles<S> {
// 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<T: Into<PathBuf>>(dir: T, pool: CpuPool) -> StaticFiles<S> {
let dir = dir.into(); let dir = dir.into();
let (dir, access) = match dir.canonicalize() { let (dir, access) = match dir.canonicalize() {
@ -468,9 +496,6 @@ impl<S: 'static> StaticFiles<S> {
} }
}; };
// use default CpuPool
let pool = { DEFAULT_CPUPOOL.lock().unwrap().clone() };
StaticFiles { StaticFiles {
directory: dir, directory: dir,
accessible: access, accessible: access,