Migrate actix-threadpool to std::future

This commit is contained in:
Michal Hornický 2019-09-14 12:38:14 +02:00 committed by Michal Hornický
parent aa9bbe2114
commit b410a5e7a1
2 changed files with 18 additions and 24 deletions

View File

@ -19,7 +19,7 @@ path = "src/lib.rs"
[dependencies] [dependencies]
derive_more = "0.15" derive_more = "0.15"
futures = "0.1.25" futures = { package = "futures-preview", version = "0.3.0-alpha.18" }
parking_lot = "0.9" parking_lot = "0.9"
lazy_static = "1.2" lazy_static = "1.2"
log = "0.4" log = "0.4"

View File

@ -1,12 +1,13 @@
//! Thread pool for blocking operations //! Thread pool for blocking operations
use std::fmt; use std::future::Future;
use std::task::{Poll,Context};
use derive_more::Display; use derive_more::Display;
use futures::sync::oneshot; use futures::channel::oneshot;
use futures::{Async, Future, Poll};
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";
@ -41,20 +42,15 @@ thread_local! {
/// Blocking operation execution error /// Blocking operation execution error
#[derive(Debug, Display)] #[derive(Debug, Display)]
pub enum BlockingError<E: fmt::Debug> { #[display(fmt = "Thread pool is gone")]
#[display(fmt = "{:?}", _0)] pub struct Cancelled;
Error(E),
#[display(fmt = "Thread pool is gone")]
Canceled,
}
/// Execute blocking function on a thread pool, returns future that resolves /// Execute blocking function on a thread pool, returns future that resolves
/// to result of the function execution. /// to result of the function execution.
pub fn run<F, I, E>(f: F) -> CpuFuture<I, E> pub fn run<F, I>(f: F) -> CpuFuture<I>
where where
F: FnOnce() -> Result<I, E> + Send + 'static, F: FnOnce() -> I + Send + 'static,
I: Send + 'static, I: Send + 'static,
E: Send + fmt::Debug + 'static,
{ {
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
POOL.with(|pool| { POOL.with(|pool| {
@ -70,19 +66,17 @@ where
/// Blocking operation completion future. It resolves with results /// Blocking operation completion future. It resolves with results
/// of blocking function execution. /// of blocking function execution.
pub struct CpuFuture<I, E> { pub struct CpuFuture<I> {
rx: oneshot::Receiver<Result<I, E>>, rx: oneshot::Receiver<I>,
} }
impl<I, E: fmt::Debug> Future for CpuFuture<I, E> { impl<I> Future for CpuFuture<I> {
type Item = I; type Output = Result<I,Cancelled>;
type Error = BlockingError<E>;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let res = futures::try_ready!(self.rx.poll().map_err(|_| BlockingError::Canceled)); let rx = Pin::new(&mut Pin::get_mut(self).rx);
match res { let res = futures::ready!(rx.poll(cx));
Ok(val) => Ok(Async::Ready(val)), Poll::Ready(res.map_err(|_| Cancelled))
Err(err) => Err(BlockingError::Error(err)),
}
} }
} }