mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-24 00:01:11 +01:00
remove actix-threadpool crate
This commit is contained in:
parent
d4c46b7da9
commit
f07d807707
@ -6,7 +6,6 @@ members = [
|
|||||||
"actix-rt",
|
"actix-rt",
|
||||||
"actix-server",
|
"actix-server",
|
||||||
"actix-service",
|
"actix-service",
|
||||||
"actix-threadpool",
|
|
||||||
"actix-tls",
|
"actix-tls",
|
||||||
"actix-tracing",
|
"actix-tracing",
|
||||||
"actix-utils",
|
"actix-utils",
|
||||||
@ -20,7 +19,6 @@ actix-router = { path = "actix-router" }
|
|||||||
actix-rt = { path = "actix-rt" }
|
actix-rt = { path = "actix-rt" }
|
||||||
actix-server = { path = "actix-server" }
|
actix-server = { path = "actix-server" }
|
||||||
actix-service = { path = "actix-service" }
|
actix-service = { path = "actix-service" }
|
||||||
actix-threadpool = { path = "actix-threadpool" }
|
|
||||||
actix-tls = { path = "actix-tls" }
|
actix-tls = { path = "actix-tls" }
|
||||||
actix-tracing = { path = "actix-tracing" }
|
actix-tracing = { path = "actix-tracing" }
|
||||||
actix-utils = { path = "actix-utils" }
|
actix-utils = { path = "actix-utils" }
|
||||||
|
@ -1,49 +0,0 @@
|
|||||||
# Changes
|
|
||||||
|
|
||||||
## [0.3.3] - 2020-07-14
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
* Update parking_lot to 0.11
|
|
||||||
|
|
||||||
## [0.3.2] - 2020-05-20
|
|
||||||
|
|
||||||
## Added
|
|
||||||
|
|
||||||
* Implement `std::error::Error` for `BlockingError` [#120]
|
|
||||||
|
|
||||||
[#120]: https://github.com/actix/actix-net/pull/120
|
|
||||||
|
|
||||||
## [0.3.1] - 2019-12-12
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
* Update parking_lot to 0.10
|
|
||||||
|
|
||||||
## [0.3.0] - 2019-12-02
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
* Expect `Result` type as a function return type
|
|
||||||
|
|
||||||
## [0.2.0] - 2019-11-21
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
* Migrate to `std::future`
|
|
||||||
|
|
||||||
## [0.1.2] - 2019-08-05
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
* Update `derive_more` to 0.15
|
|
||||||
|
|
||||||
* Update `parking_lot` to 0.9
|
|
||||||
|
|
||||||
## [0.1.1] - 2019-06-05
|
|
||||||
|
|
||||||
* Update parking_lot
|
|
||||||
|
|
||||||
## [0.1.0] - 2019-03-28
|
|
||||||
|
|
||||||
* Move threadpool to separate crate
|
|
@ -1,27 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "actix-threadpool"
|
|
||||||
version = "0.3.3"
|
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
|
||||||
description = "Actix thread pool for sync code"
|
|
||||||
keywords = ["actix", "network", "framework", "async", "futures"]
|
|
||||||
homepage = "https://actix.rs"
|
|
||||||
repository = "https://github.com/actix/actix-net.git"
|
|
||||||
documentation = "https://docs.rs/actix-threadpool/"
|
|
||||||
categories = ["network-programming", "asynchronous"]
|
|
||||||
license = "MIT OR Apache-2.0"
|
|
||||||
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
|
||||||
edition = "2018"
|
|
||||||
workspace = ".."
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "actix_threadpool"
|
|
||||||
path = "src/lib.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
derive_more = "0.99.2"
|
|
||||||
futures-channel = "0.3.7"
|
|
||||||
parking_lot = "0.11"
|
|
||||||
lazy_static = "1.3"
|
|
||||||
log = "0.4"
|
|
||||||
num_cpus = "1.10"
|
|
||||||
threadpool = "1.7"
|
|
@ -1 +0,0 @@
|
|||||||
../LICENSE-APACHE
|
|
@ -1 +0,0 @@
|
|||||||
../LICENSE-MIT
|
|
@ -1,96 +0,0 @@
|
|||||||
//! Thread pool for blocking operations
|
|
||||||
|
|
||||||
#![deny(rust_2018_idioms, nonstandard_style)]
|
|
||||||
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
|
||||||
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
|
||||||
|
|
||||||
use std::fmt;
|
|
||||||
use std::future::Future;
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::task::{Context, Poll};
|
|
||||||
|
|
||||||
use derive_more::Display;
|
|
||||||
use futures_channel::oneshot;
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
use threadpool::ThreadPool;
|
|
||||||
|
|
||||||
/// Env variable for default cpu pool size.
|
|
||||||
const ENV_CPU_POOL_VAR: &str = "ACTIX_THREADPOOL";
|
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
|
||||||
pub(crate) static ref DEFAULT_POOL: Mutex<ThreadPool> = {
|
|
||||||
let num = std::env::var(ENV_CPU_POOL_VAR)
|
|
||||||
.map_err(|_| ())
|
|
||||||
.and_then(|val| {
|
|
||||||
val.parse().map_err(|_| log::warn!(
|
|
||||||
"Can not parse {} value, using default",
|
|
||||||
ENV_CPU_POOL_VAR,
|
|
||||||
))
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|_| num_cpus::get() * 5);
|
|
||||||
Mutex::new(
|
|
||||||
threadpool::Builder::new()
|
|
||||||
.thread_name("actix-web".to_owned())
|
|
||||||
.num_threads(num)
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
thread_local! {
|
|
||||||
static POOL: ThreadPool = {
|
|
||||||
DEFAULT_POOL.lock().clone()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Blocking operation execution error
|
|
||||||
#[derive(Debug, Display)]
|
|
||||||
pub enum BlockingError<E: fmt::Debug> {
|
|
||||||
#[display(fmt = "{:?}", _0)]
|
|
||||||
Error(E),
|
|
||||||
#[display(fmt = "Thread pool is gone")]
|
|
||||||
Canceled,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<E: fmt::Debug> std::error::Error for BlockingError<E> {}
|
|
||||||
|
|
||||||
/// Execute blocking function on a thread pool, returns future that resolves
|
|
||||||
/// to result of the function execution.
|
|
||||||
pub fn run<F, I, E>(f: F) -> CpuFuture<I, E>
|
|
||||||
where
|
|
||||||
F: FnOnce() -> Result<I, E> + Send + 'static,
|
|
||||||
I: Send + 'static,
|
|
||||||
E: Send + fmt::Debug + 'static,
|
|
||||||
{
|
|
||||||
let (tx, rx) = oneshot::channel();
|
|
||||||
POOL.with(|pool| {
|
|
||||||
pool.execute(move || {
|
|
||||||
if !tx.is_canceled() {
|
|
||||||
let _ = tx.send(f());
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
CpuFuture { rx }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Blocking operation completion future. It resolves with results
|
|
||||||
/// of blocking function execution.
|
|
||||||
pub struct CpuFuture<I, E> {
|
|
||||||
rx: oneshot::Receiver<Result<I, E>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<I, E: fmt::Debug> Future for CpuFuture<I, E> {
|
|
||||||
type Output = Result<I, BlockingError<E>>;
|
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
let rx = Pin::new(&mut self.rx);
|
|
||||||
let res = match rx.poll(cx) {
|
|
||||||
Poll::Pending => return Poll::Pending,
|
|
||||||
Poll::Ready(res) => res
|
|
||||||
.map_err(|_| BlockingError::Canceled)
|
|
||||||
.and_then(|res| res.map_err(BlockingError::Error)),
|
|
||||||
};
|
|
||||||
Poll::Ready(res)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user