feat: worker threads multiplier option --threads-multiplier
A new -t, --threads-multiplier <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.
Diff
src/bin/server.rs | 22 +++++++++++++++++++---
src/core/config.rs | 12 ++++++++++++
2 files changed, 31 insertions(+), 3 deletions(-)
@@ -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(())
}
@@ -11,6 +11,18 @@ pub struct Options {
pub port: u16,
#[structopt(
long,
short = "t",
default_value = "8",
env = "SERVER_THREADS_MULTIPLIER"
)]
pub threads_multiplier: usize,
#[structopt(long, short = "r", default_value = "./public", env = "SERVER_ROOT")]
pub root: String,