From 14c973f72fac29ea4d8e0669b8671152cffdc72a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 10 Feb 2024 23:07:59 +0100 Subject: [PATCH] udp: Config.socket_workers: make value 0 auto-use available vCPUs --- crates/udp/src/config.rs | 4 +++- crates/udp/src/lib.rs | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/udp/src/config.rs b/crates/udp/src/config.rs index b25eae9..ae97a4f 100644 --- a/crates/udp/src/config.rs +++ b/crates/udp/src/config.rs @@ -11,7 +11,9 @@ use aquatic_toml_config::TomlConfig; #[derive(Clone, Debug, PartialEq, TomlConfig, Deserialize, Serialize)] #[serde(default, deny_unknown_fields)] pub struct Config { - /// Number of socket workers. One per virtual CPU is recommended + /// Number of socket workers + /// + /// 0 = automatically set to number of available virtual CPUs pub socket_workers: usize, pub log_level: LogLevel, pub network: NetworkConfig, diff --git a/crates/udp/src/lib.rs b/crates/udp/src/lib.rs index e91027e..b1215c1 100644 --- a/crates/udp/src/lib.rs +++ b/crates/udp/src/lib.rs @@ -3,7 +3,7 @@ pub mod config; pub mod swarm; pub mod workers; -use std::thread::{sleep, Builder, JoinHandle}; +use std::thread::{available_parallelism, sleep, Builder, JoinHandle}; use std::time::Duration; use anyhow::Context; @@ -22,9 +22,13 @@ use workers::socket::ConnectionValidator; pub const APP_NAME: &str = "aquatic_udp: UDP BitTorrent tracker"; pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); -pub fn run(config: Config) -> ::anyhow::Result<()> { +pub fn run(mut config: Config) -> ::anyhow::Result<()> { let mut signals = Signals::new([SIGUSR1])?; + if config.socket_workers == 0 { + config.socket_workers = available_parallelism().map(Into::into).unwrap_or(1); + }; + let state = State::default(); let statistics = Statistics::new(&config); let connection_validator = ConnectionValidator::new(&config)?;