index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-01-20 0:20:45.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-01-20 0:20:45.0 +00:00:00
commit
360ae996c5366c6ab03b476ffc903728acebb58a [patch]
tree
a73da8f7a49e2375fb37e7b91337e34810682d15
parent
9867d71749c6113e65b03dae370e90f33406b2df
download
360ae996c5366c6ab03b476ffc903728acebb58a.tar.gz

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(-)

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,