index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-04-30 10:40:22.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-04-30 10:40:22.0 +00:00:00
commit
04ec1b1fd65383ba787e008670ff065085d820bd [patch]
tree
4440181cde4374b778053c1b10d07a6174b8702a
parent
6ed3fe5250129feba03211188d6901a87a5a0c1e
download
04ec1b1fd65383ba787e008670ff065085d820bd.tar.gz

refactor: one thread per core by default



Diff

 README.md     | 30 +++++++++++++++---------------
 src/config.rs |  4 ++--
 src/server.rs |  6 ++++--
 3 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/README.md b/README.md
index 69a6a2e..9f3840f 100644
--- a/README.md
+++ b/README.md
@@ -40,19 +40,19 @@ Server can be configured either via environment variables or their equivalent co

### Environment Variables

| Variable                    | Description                                                                                                                                                                                                                                                                                                                                                                                        | Default                                                                                                                         |
| --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `SERVER_HOST`               | Host address (E.g 127.0.0.1).                                                                                                                                                                                                                                                                                                                                                                      | Default `[::]`.                                                                                                                 |
| `SERVER_PORT`               | Host port.                                                                                                                                                                                                                                                                                                                                                                                         | Default `80`.                                                                                                                   |
| `SERVER_ROOT`               | Root directory path of static                                                                                                                                                                                                                                                                                                                                                                      | Default `./public`.                                                                                                             |
| `SERVER_LOG_LEVEL`          | Specify a logging level in lower case. (Values `error`, `warn`, `info`, `debug`, `trace`).                                                                                                                                                                                                                                                                                                         | Default `error`                                                                                                                 |
| `SERVER_ERROR_PAGE_404`     | HTML file path for 404 errors.                                                                                                                                                                                                                                                                                                                                                                     | If path is not specified or simply don't exists then server will use a generic HTML error message. Default `./public/404.html`. |
| `SERVER_ERROR_PAGE_50X`     | HTML file path for 50x errors.                                                                                                                                                                                                                                                                                                                                                                     | If path is not specified or simply don't exists then server will use a generic HTML error message. Default `./public/50x.html`  |
| `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. | Default `8`                                                                                                                     |
| `SERVER_HTTP2_TLS`          | Enable HTTP/2 with TLS support. Make sure also to adjust current server port.                                                                                                                                                                                                                                                                                                                      | Default `false`                                                                                                                 |
| `SERVER_HTTP2_TLS_CERT`     | Specify the file path to read the certificate.                                                                                                                                                                                                                                                                                                                                                     | Default empty                                                                                                                   |
| `SERVER_HTTP2_TLS_KEY`      | Specify the file path to read the private key.                                                                                                                                                                                                                                                                                                                                                     | Default empty                                                                                                                   |
| `SERVER_CORS_ALLOW_ORIGINS` | Specify a optional CORS list of allowed origin hosts separated by comas. Host ports or protocols aren't being checked. Use an asterisk (*) to allow any host.                                                                                                                                                                                                                                      | Default empty (which means CORS is disabled)                                                                                    |
| Variable | Description | Default |
| --- | --- | --- |
| `SERVER_HOST` | Host address (E.g 127.0.0.1). | Default `[::]`. |
| `SERVER_PORT` | Host port. | Default `80`. |
| `SERVER_ROOT` | Root directory path of static | Default `./public`. |
| `SERVER_LOG_LEVEL`          | Specify a logging level in lower case. (Values `error`, `warn`, `info`, `debug`, `trace`). | Default `error` |
| `SERVER_ERROR_PAGE_404`     | HTML file path for 404 errors. | If path is not specified or simply don't exists then server will use a generic HTML error message. Default `./public/404.html`. |
| `SERVER_ERROR_PAGE_50X`     | HTML file path for 50x errors. | If path is not specified or simply don't exists then server will use a generic HTML error message. Default `./public/50x.html`  |
| `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. | Default one thread per core. |
| `SERVER_HTTP2_TLS` | Enable HTTP/2 with TLS support. Make sure also to adjust current server port. | Default `false` |
| `SERVER_HTTP2_TLS_CERT`     | Specify the file path to read the certificate. | Default empty |
| `SERVER_HTTP2_TLS_KEY`      | Specify the file path to read the private key. | Default empty |
| `SERVER_CORS_ALLOW_ORIGINS` | Specify a optional CORS list of allowed origin hosts separated by comas. Host ports or protocols aren't being checked. Use an asterisk (*) to allow any host. | Default empty (which means CORS is disabled) |

### Command-line arguments

@@ -99,9 +99,9 @@ OPTIONS:
    -n, --threads-multiplier <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
            or 1 then one thread per core 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 [env: SERVER_THREADS_MULTIPLIER=]
            [default: 8]
            [default: 1]
```

## Docker stack
diff --git a/src/config.rs b/src/config.rs
index b3240c5..ca29b26 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -17,12 +17,12 @@ pub struct Config {
    #[structopt(
        long,
        short = "n",
        default_value = "8",
        default_value = "1",
        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.
    /// When multiplier value is 0 or 1 then one thread per core 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,

diff --git a/src/server.rs b/src/server.rs
index 82946a9..bdb9eb8 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -23,9 +23,11 @@ impl Server {
        CONFIG.set(Config::from_args()).unwrap();
        let opts = Config::global();

        // Configure number of worker threads
        let cpus = num_cpus::get();
        let threads = match opts.threads_multiplier {
            0 | 1 => 1,
            _ => num_cpus::get() * opts.threads_multiplier,
            0 | 1 => cpus,
            n => cpus * n,
        };

        Self { threads }