1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-28 15:40:36 +02:00

remove num_cpus dependency (#488)

This commit is contained in:
Rob Ede
2023-08-24 00:56:31 +01:00
committed by GitHub
parent 9cb8a1fadc
commit 6ce8307060
6 changed files with 115 additions and 49 deletions

View File

@ -1,4 +1,4 @@
use std::{io, time::Duration};
use std::{io, num::NonZeroUsize, time::Duration};
use actix_rt::net::TcpStream;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
@ -55,7 +55,7 @@ impl ServerBuilder {
let (cmd_tx, cmd_rx) = unbounded_channel();
ServerBuilder {
threads: num_cpus::get_physical(),
threads: std::thread::available_parallelism().map_or(2, NonZeroUsize::get),
token: 0,
factories: Vec::new(),
sockets: Vec::new(),
@ -76,6 +76,12 @@ impl ServerBuilder {
/// The default worker count is the number of physical CPU cores available. If your benchmark
/// testing indicates that simultaneous multi-threading is beneficial to your app, you can use
/// the [`num_cpus`] crate to acquire the _logical_ core count instead.
///
/// # Panics
///
/// Panics if `num` is 0.
///
/// [`num_cpus`]: https://docs.rs/num_cpus
pub fn workers(mut self, num: usize) -> Self {
assert_ne!(num, 0, "workers must be greater than 0");
self.threads = num;

View File

@ -1,6 +1,7 @@
use std::{
future::Future,
io, mem,
num::NonZeroUsize,
pin::Pin,
rc::Rc,
sync::{
@ -249,8 +250,11 @@ pub(crate) struct ServerWorkerConfig {
impl Default for ServerWorkerConfig {
fn default() -> Self {
// 512 is the default max blocking thread count of tokio runtime.
let max_blocking_threads = std::cmp::max(512 / num_cpus::get_physical(), 1);
let parallelism = std::thread::available_parallelism().map_or(2, NonZeroUsize::get);
// 512 is the default max blocking thread count of a Tokio runtime.
let max_blocking_threads = std::cmp::max(512 / parallelism, 1);
Self {
shutdown_timeout: Duration::from_secs(30),
max_blocking_threads,