Cosmetic refactor

- turn log::error! into log::warn! as it doesn't throw any error
- add Clone and Copy impls for Cancelled making it cheap to operate with
- apply rustfmt
This commit is contained in:
tyranron 2019-11-10 18:01:27 +02:00
parent 5790da2d8e
commit 7d58446cb7
No known key found for this signature in database
GPG Key ID: 762E144FB230A4F0
1 changed files with 20 additions and 21 deletions

View File

@ -1,34 +1,34 @@
//! Thread pool for blocking operations //! Thread pool for blocking operations
use std::future::Future; use std::{
use std::task::{Poll,Context}; future::Future,
pin::Pin,
task::{Context, Poll},
};
use derive_more::Display; use derive_more::Display;
use futures::channel::oneshot; use futures::channel::oneshot;
use parking_lot::Mutex; use parking_lot::Mutex;
use threadpool::ThreadPool; use threadpool::ThreadPool;
use std::pin::Pin;
/// Env variable for default cpu pool size /// Env variable for default cpu pool size.
const ENV_CPU_POOL_VAR: &str = "ACTIX_THREADPOOL"; const ENV_CPU_POOL_VAR: &str = "ACTIX_THREADPOOL";
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub(crate) static ref DEFAULT_POOL: Mutex<ThreadPool> = { pub(crate) static ref DEFAULT_POOL: Mutex<ThreadPool> = {
let default = match std::env::var(ENV_CPU_POOL_VAR) { let num = std::env::var(ENV_CPU_POOL_VAR)
Ok(val) => { .map_err(|_| ())
if let Ok(val) = val.parse() { .and_then(|val| {
val val.parse().map_err(|_| log::warn!(
} else { "Can not parse {} value, using default",
log::error!("Can not parse ACTIX_THREADPOOL value"); ENV_CPU_POOL_VAR,
num_cpus::get() * 5 ))
} })
} .unwrap_or_else(|_| num_cpus::get() * 5);
Err(_) => num_cpus::get() * 5,
};
Mutex::new( Mutex::new(
threadpool::Builder::new() threadpool::Builder::new()
.thread_name("actix-web".to_owned()) .thread_name("actix-web".to_owned())
.num_threads(default) .num_threads(num)
.build(), .build(),
) )
}; };
@ -40,8 +40,8 @@ thread_local! {
}; };
} }
/// Blocking operation execution error /// Error of blocking operation execution being cancelled.
#[derive(Debug, Display)] #[derive(Clone, Copy, Debug, Display)]
#[display(fmt = "Thread pool is gone")] #[display(fmt = "Thread pool is gone")]
pub struct Cancelled; pub struct Cancelled;
@ -71,12 +71,11 @@ pub struct CpuFuture<I> {
} }
impl<I> Future for CpuFuture<I> { impl<I> Future for CpuFuture<I> {
type Output = Result<I,Cancelled>; type Output = Result<I, Cancelled>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let rx = Pin::new(&mut Pin::get_mut(self).rx); let rx = Pin::new(&mut Pin::get_mut(self).rx);
let res = futures::ready!(rx.poll(cx)); let res = futures::ready!(rx.poll(cx));
Poll::Ready(res.map_err(|_| Cancelled)) Poll::Ready(res.map_err(|_| Cancelled))
} }
} }