mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
refactor edfault cpu pool
This commit is contained in:
parent
a5bbc455c0
commit
ef15646bd7
@ -77,6 +77,7 @@ time = "0.1"
|
|||||||
encoding = "0.2"
|
encoding = "0.2"
|
||||||
language-tags = "0.2"
|
language-tags = "0.2"
|
||||||
lazy_static = "1.0"
|
lazy_static = "1.0"
|
||||||
|
parking_lot = "0.5"
|
||||||
url = { version="1.7", features=["query_encoding"] }
|
url = { version="1.7", features=["query_encoding"] }
|
||||||
cookie = { version="0.10", features=["percent-encode"] }
|
cookie = { version="0.10", features=["percent-encode"] }
|
||||||
brotli2 = { version="^0.3.2", optional = true }
|
brotli2 = { version="^0.3.2", optional = true }
|
||||||
|
28
src/fs.rs
28
src/fs.rs
@ -4,9 +4,8 @@ use std::fs::{DirEntry, File, Metadata};
|
|||||||
use std::io::{Read, Seek};
|
use std::io::{Read, Seek};
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::Mutex;
|
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
use std::{cmp, env, io};
|
use std::{cmp, io};
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::os::unix::fs::MetadataExt;
|
use std::os::unix::fs::MetadataExt;
|
||||||
@ -25,9 +24,7 @@ use httpmessage::HttpMessage;
|
|||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
use param::FromParam;
|
use param::FromParam;
|
||||||
|
use server::settings::DEFAULT_CPUPOOL;
|
||||||
/// Env variable for default cpu pool size for `StaticFiles`
|
|
||||||
const ENV_CPU_POOL_VAR: &str = "ACTIX_FS_POOL";
|
|
||||||
|
|
||||||
/// Return the MIME type associated with a filename extension (case-insensitive).
|
/// 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
|
/// 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,
|
_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> {
|
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.
|
/// `StaticFile` uses `CpuPool` for blocking filesystem operations.
|
||||||
/// By default pool with 20 threads is used.
|
/// 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> {
|
pub fn new<T: Into<PathBuf>>(dir: T) -> StaticFiles<S> {
|
||||||
// use default CpuPool
|
// use default CpuPool
|
||||||
let pool = { DEFAULT_CPUPOOL.lock().unwrap().clone() };
|
let pool = { DEFAULT_CPUPOOL.lock().clone() };
|
||||||
|
|
||||||
StaticFiles::with_pool(dir, pool)
|
StaticFiles::with_pool(dir, pool)
|
||||||
}
|
}
|
||||||
|
@ -112,6 +112,7 @@ extern crate mime;
|
|||||||
extern crate mime_guess;
|
extern crate mime_guess;
|
||||||
extern crate mio;
|
extern crate mio;
|
||||||
extern crate net2;
|
extern crate net2;
|
||||||
|
extern crate parking_lot;
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
extern crate slab;
|
extern crate slab;
|
||||||
extern crate tokio;
|
extern crate tokio;
|
||||||
|
@ -15,7 +15,7 @@ mod h1writer;
|
|||||||
mod h2;
|
mod h2;
|
||||||
mod h2writer;
|
mod h2writer;
|
||||||
pub(crate) mod helpers;
|
pub(crate) mod helpers;
|
||||||
mod settings;
|
pub(crate) mod settings;
|
||||||
pub(crate) mod shared;
|
pub(crate) mod shared;
|
||||||
mod srv;
|
mod srv;
|
||||||
pub(crate) mod utils;
|
pub(crate) mod utils;
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
use std::cell::{Cell, RefCell, RefMut, UnsafeCell};
|
use std::cell::{Cell, RefCell, RefMut, UnsafeCell};
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::{fmt, mem, net};
|
use std::{env, fmt, mem, net};
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures_cpupool::{Builder, CpuPool};
|
use futures_cpupool::CpuPool;
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
|
use parking_lot::Mutex;
|
||||||
use time;
|
use time;
|
||||||
|
|
||||||
use super::channel::Node;
|
use super::channel::Node;
|
||||||
@ -15,6 +16,26 @@ use super::KeepAlive;
|
|||||||
use body::Body;
|
use body::Body;
|
||||||
use httpresponse::{HttpResponse, HttpResponseBuilder, HttpResponsePool};
|
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
|
/// Various server settings
|
||||||
pub struct ServerSettings {
|
pub struct ServerSettings {
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
@ -106,7 +127,8 @@ impl ServerSettings {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let val = &mut *self.cpu_pool.get();
|
let val = &mut *self.cpu_pool.get();
|
||||||
if val.is_none() {
|
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()
|
val.as_ref().unwrap()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user