1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 00:01:11 +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); let addr = ("127.0.0.1", 8080);
info!("starting server on port: {}", &addr.0); info!("starting server on port: {}", &addr.0);
// Bind socket address and start worker(s). By default, the server uses the number of available // Bind socket address and start worker(s). By default, the server uses the number of physical
// logical CPU cores as the worker count. For this reason, the closure passed to bind needs // CPU cores as the worker count. For this reason, the closure passed to bind needs to return
// to return a service *factory*; so it can be created once per worker. // a service *factory*; so it can be created once per worker.
Server::build() Server::build()
.bind("echo", addr, move || { .bind("echo", addr, move || {
let count = Arc::clone(&count); let count = Arc::clone(&count);

View File

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

View File

@ -250,7 +250,7 @@ pub(crate) struct ServerWorkerConfig {
impl Default for ServerWorkerConfig { impl Default for ServerWorkerConfig {
fn default() -> Self { fn default() -> Self {
// 512 is the default max blocking thread count of tokio runtime. // 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 { Self {
shutdown_timeout: Duration::from_secs(30), shutdown_timeout: Duration::from_secs(30),
max_blocking_threads, max_blocking_threads,