1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-23 21:51:06 +01:00

use "physical" cpu cores as default worker count

This commit is contained in:
Rob Ede 2021-12-08 05:37:06 +00:00
parent ba901c70df
commit 3a3d654cea
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 10 additions and 7 deletions

View File

@ -33,9 +33,9 @@ async fn run() -> io::Result<()> {
let addr = ("127.0.0.1", 8080);
info!("starting server on port: {}", &addr.0);
// Bind socket address and start worker(s). By default, the server uses the number of available
// logical CPU cores as the worker count. For this reason, the closure passed to bind needs
// to return a service *factory*; so it can be created once per worker.
// Bind socket address and start worker(s). By default, the server uses the number of physical
// CPU cores as the worker count. For this reason, the closure passed to bind needs to return
// a service *factory*; so it can be created once per worker.
Server::build()
.bind("echo", addr, move || {
let count = Arc::clone(&count);

View File

@ -40,7 +40,7 @@ impl ServerBuilder {
let (cmd_tx, cmd_rx) = unbounded_channel();
ServerBuilder {
threads: num_cpus::get(),
threads: num_cpus::get_physical(),
token: 0,
factories: Vec::new(),
sockets: Vec::new(),
@ -55,8 +55,11 @@ impl ServerBuilder {
/// Set number of workers to start.
///
/// By default server uses number of available logical CPU as workers count. Workers must be
/// greater than 0.
/// `num` must be greater than 0.
///
/// 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.
pub fn workers(mut self, num: usize) -> Self {
assert_ne!(num, 0, "workers must be greater than 0");
self.threads = num;

View File

@ -250,7 +250,7 @@ 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(), 1);
let max_blocking_threads = std::cmp::max(512 / num_cpus::get_physical(), 1);
Self {
shutdown_timeout: Duration::from_secs(30),
max_blocking_threads,