Skip to content

Commit 538c1ac

Browse files
author
happybeing
committed
Merge pull request 'dweb concurrency (more in default and configurable worker threads)' (#28) from riddim/dweb:concurrency2 into main
Reviewed-on: https://codeberg.org/happybeing/dweb/pulls/28
2 parents 05680d9 + 778291f commit 538c1ac

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ To see the list of website names available:
4646
dweb list-names
4747
```
4848

49+
### Server concurrency (workers)
50+
51+
The dweb HTTP server (Actix) runs with a configurable number of worker threads.
52+
53+
- Environment variable: `DWEB_WORKERS`
54+
- Default: `12`
55+
- Example:
56+
```bash
57+
DWEB_WORKERS=24 dweb
58+
```
59+
60+
Notes:
61+
- More workers can increase throughput for I/O-bound workloads at the cost of CPU/RAM/file descriptors.
62+
- A good starting point is near the number of logical CPU cores. Measure and adjust.
63+
4964
## Status and Documentation
5065
The dweb library is a work in progress so expect breaking changes expecially in newly added features. The web publishing format and command line interface are more stable but breaking changes are still possible.
5166

dweb-cli/src/cli_options.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ use dweb::web::name::validate_dweb_name;
4343
author,
4444
version,
4545
about,
46-
long_about = "a web publishing and browsing app for Autonomi peer-to-peer network"
46+
long_about = "a web publishing and browsing app for Autonomi peer-to-peer network",
47+
after_help = "Server concurrency: set maximum number of parallel requests via DWEB_WORKERS (default: 12). Example: DWEB_WORKERS=24 dweb serve"
4748
)]
4849
pub struct Opt {
4950
/// Connect to the alpha public network
@@ -145,6 +146,7 @@ fn greater_than_0(s: &str) -> Result<u64, String> {
145146
#[derive(Subcommand, Debug)]
146147
pub enum Subcommands {
147148
/// Start a server to view Autonomi websites in your browser. Also required for some dweb subcommands.
149+
#[command(after_help = "Server concurrency: set maximum number of parallel requests via DWEB_WORKERS (default: 12). Example: DWEB_WORKERS=24 dweb serve")]
148150
///
149151
/// Afterwards, if you open a new terminal you can view a website in your browser
150152
/// by typing 'dweb open awesome'. The 'awesome' website contains links to other websites.

dweb-server/src/services.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ pub async fn init_dweb_server_blocking(
122122
let port = client_config.port.unwrap_or(SERVER_PORTS_MAIN_PORT);
123123
let client = client.clone();
124124

125-
let server = HttpServer::new(move || {
125+
// Determine number of Actix workers from env var DWEB_WORKERS (default 12)
126+
let workers = get_worker_count_from_env();
127+
128+
let http_server = HttpServer::new(move || {
126129
App::new()
127130
.wrap(
128131
actix_cors::Cors::default()
@@ -221,9 +224,9 @@ pub async fn init_dweb_server_blocking(
221224
.app_data(Data::new(is_main_server))
222225
.into_app()
223226
})
224-
.keep_alive(Duration::from_secs(crate::services::CONNECTION_TIMEOUT));
225-
226-
let http_server = server.bind((host.clone(), port));
227+
.keep_alive(Duration::from_secs(crate::services::CONNECTION_TIMEOUT))
228+
.workers(workers)
229+
.bind((host.clone(), port));
227230
match http_server {
228231
Ok(server) => {
229232
match directory_version_with_port_copy2 {
@@ -252,3 +255,17 @@ pub async fn init_dweb_server_blocking(
252255
}
253256
}
254257
}
258+
259+
const DEFAULT_WORKERS: usize = 12;
260+
261+
fn parse_workers(value: Option<String>) -> usize {
262+
match value.and_then(|s| s.trim().parse::<usize>().ok()) {
263+
Some(n) if n >= 1 => n,
264+
Some(_) => 1, // clamp to minimum of 1
265+
None => DEFAULT_WORKERS,
266+
}
267+
}
268+
269+
fn get_worker_count_from_env() -> usize {
270+
parse_workers(std::env::var("DWEB_WORKERS").ok())
271+
}

0 commit comments

Comments
 (0)