Migrate actix-threadpool to std::future (#59)

* Migrate actix-threadpool to std::future

* 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

* Bump up crate version to 0.2.0 and pre-fill its changelog

* Disable patching 'actix-threadpool' crate in global workspace as unnecessary

* Revert patching and fix 'actix-rt'
This commit is contained in:
Kai Ren 2019-11-10 19:43:02 +02:00 committed by Nikolay Kim
parent 8c09b1e7ae
commit 05ae2585f3
5 changed files with 31 additions and 27 deletions

View File

@ -18,7 +18,7 @@ name = "actix_rt"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
actix-threadpool = "0.1.1" actix-threadpool = "0.2"
futures = "0.3.1" futures = "0.3.1"
# TODO: Replace this with dependency on tokio-runtime once it is ready # TODO: Replace this with dependency on tokio-runtime once it is ready

View File

@ -4,11 +4,11 @@ use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use std::{fmt, thread}; use std::{fmt, thread};
use std::pin::Pin; use std::pin::Pin;
use std::task::Context; use std::task::{Context, Poll};
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender}; use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
use futures::channel::oneshot::{channel, Canceled, Sender}; use futures::channel::oneshot::{channel, Canceled, Sender};
use futures::{future, Future, Poll, FutureExt, Stream}; use futures::{future, Future, FutureExt, Stream};
use tokio::runtime::current_thread::spawn; use tokio::runtime::current_thread::spawn;
use crate::builder::Builder; use crate::builder::Builder;

View File

@ -1,5 +1,11 @@
# Changes # Changes
## [0.2.0] - 2019-??-??
### Changed
* Migrate to `std::future`
## [0.1.2] - 2019-08-05 ## [0.1.2] - 2019-08-05
### Changed ### Changed

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-threadpool" name = "actix-threadpool"
version = "0.1.2" version = "0.2.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix thread pool for sync code" description = "Actix thread pool for sync code"
keywords = ["actix", "network", "framework", "async", "futures"] keywords = ["actix", "network", "framework", "async", "futures"]

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,13 +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 = unsafe{ self.map_unchecked_mut(|s|&mut s.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))
} }
} }