1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

refactor edfault cpu pool

This commit is contained in:
Nikolay Kim 2018-06-18 04:56:18 +06:00
parent a5bbc455c0
commit ef15646bd7
5 changed files with 32 additions and 28 deletions

View File

@ -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 }

View File

@ -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<S> {
_follow_symlinks: bool,
}
lazy_static! {
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> {
/// 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<T: Into<PathBuf>>(dir: T) -> StaticFiles<S> {
// use default CpuPool
let pool = { DEFAULT_CPUPOOL.lock().unwrap().clone() };
let pool = { DEFAULT_CPUPOOL.lock().clone() };
StaticFiles::with_pool(dir, pool)
}

View File

@ -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;

View File

@ -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;

View File

@ -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<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_CPU_POOL value");
20
}
}
Err(_) => 20,
};
Mutex::new(CpuPool::new(default))
};
}
/// Various server settings
pub struct ServerSettings {
addr: Option<net::SocketAddr>,
@ -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()
}