From 360ae996c5366c6ab03b476ffc903728acebb58a Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Wed, 20 Jan 2021 01:20:45 +0100 Subject: [PATCH] feat: worker threads multiplier option --threads-multiplier A new -t, --threads-multiplier option which represent a number of worker threads multiplier that'll be multiplied by the number of system CPUs using the formula: worker threads = number of CPUs * n where `n` is the value that changes with this option. When the multiplier value is 0 or 1 then the `number of CPUs` is only used. The number of worker threads result should be a number between 1 and 32,768 though it is advised to keep this value on the smaller side. --- src/bin/server.rs | 22 +++++++++++++++++++--- src/core/config.rs | 12 ++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/bin/server.rs b/src/bin/server.rs index 994c579..8d0292f 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -93,9 +93,25 @@ async fn server(opts: config::Options) -> Result { Ok(()) } -#[tokio::main(max_threads = 10_000)] -async fn main() -> Result { - server(config::Options::from_args()).await?; +fn main() -> Result { + let opts = config::Options::from_args(); + let n = if opts.threads_multiplier == 0 { + 1 + } else { + opts.threads_multiplier + }; + let threads = num_cpus::get() * n; + + tokio::runtime::Builder::new_multi_thread() + .worker_threads(threads) + .enable_all() + .build()? + .block_on(async { + let r = server(opts).await; + if r.is_err() { + panic!("Server error: {:?}", r.unwrap_err()) + } + }); Ok(()) } diff --git a/src/core/config.rs b/src/core/config.rs index 43a5914..63e9f23 100644 --- a/src/core/config.rs +++ b/src/core/config.rs @@ -11,6 +11,18 @@ pub struct Options { /// Host port pub port: u16, + #[structopt( + long, + short = "t", + default_value = "8", + env = "SERVER_THREADS_MULTIPLIER" + )] + /// Number of worker threads multiplier that'll be multiplied by the number of system CPUs + /// using the formula: `worker threads = number of CPUs * n` where `n` is the value that changes here. + /// When multiplier value is 0 or 1 then the `number of CPUs` is used. + /// Number of worker threads result should be a number between 1 and 32,768 though it is advised to keep this value on the smaller side. + pub threads_multiplier: usize, + #[structopt(long, short = "r", default_value = "./public", env = "SERVER_ROOT")] /// Root directory path of static files pub root: String, -- libgit2 1.7.2