From 8071c6ce3f781a7021197fa6e20f776fcebadbbf Mon Sep 17 00:00:00 2001 From: asonix Date: Wed, 26 Jul 2023 23:04:04 -0500 Subject: [PATCH] Make signature threads configurable --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 5 +++++ relay.nix | 2 +- src/config.rs | 14 ++++++++++++++ src/main.rs | 2 +- src/requests.rs | 7 +------ 7 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 68e5b29..43f4dd3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -391,7 +391,7 @@ checksum = "3b13c32d80ecc7ab747b80c3784bce54ee8a7a0cc4fbda9bf4cda2cf6fe90854" [[package]] name = "ap-relay" -version = "0.3.90" +version = "0.3.91" dependencies = [ "activitystreams", "activitystreams-ext", diff --git a/Cargo.toml b/Cargo.toml index 7ad223e..ce4e026 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ap-relay" description = "A simple activitypub relay" -version = "0.3.90" +version = "0.3.91" authors = ["asonix "] license = "AGPL-3.0" readme = "README.md" diff --git a/README.md b/README.md index e1d7733..24b1566 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ PROMETHEUS_PORT=9000 CLIENT_TIMEOUT=10 CLIENT_POOL_SIZE=20 DELIVER_CONCURRENCY=8 +SIGNATURE_THREADS=2 ``` #### Descriptions @@ -168,6 +169,10 @@ files", you can either decrease this number or increase the ulimit for your syst ##### `DELIVER_CONCURRENCY` Optional - How many deliver requests the relay should allow to be in-flight per thread. the default is 8 +##### `SIGNATURE_THREADS` +Optional - Override number of threads used for signing and verifying requests. Default is +`std::thread::available_parallelism()` (It tries to detect how many cores you have). If it cannot +detect the correct number of cores, it falls back to 1. ### Subscribing Mastodon admins can subscribe to this relay by adding the `/inbox` route to their relay settings. diff --git a/relay.nix b/relay.nix index 3d56fd3..caccc38 100644 --- a/relay.nix +++ b/relay.nix @@ -6,7 +6,7 @@ rustPlatform.buildRustPackage { pname = "relay"; - version = "0.3.90"; + version = "0.3.91"; src = ./.; cargoLock.lockFile = ./Cargo.lock; diff --git a/src/config.rs b/src/config.rs index 414de76..be46fc6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -48,6 +48,7 @@ pub(crate) struct ParsedConfig { deliver_concurrency: u64, client_timeout: u64, client_pool_size: usize, + signature_threads: Option, } #[derive(Clone)] @@ -74,6 +75,7 @@ pub struct Config { deliver_concurrency: u64, client_timeout: u64, client_pool_size: usize, + signature_threads: Option, } #[derive(Clone)] @@ -144,6 +146,7 @@ impl std::fmt::Debug for Config { .field("deliver_concurrency", &self.deliver_concurrency) .field("client_timeout", &self.client_timeout) .field("client_pool_size", &self.client_pool_size) + .field("signature_threads", &self.signature_threads) .finish() } } @@ -176,6 +179,7 @@ impl Config { .set_default("deliver_concurrency", 8u64)? .set_default("client_timeout", 10u64)? .set_default("client_pool_size", 20u64)? + .set_default("signature_threads", None as Option)? .add_source(Environment::default()) .build()?; @@ -250,6 +254,16 @@ impl Config { deliver_concurrency: config.deliver_concurrency, client_timeout: config.client_timeout, client_pool_size: config.client_pool_size, + signature_threads: config.signature_threads, + }) + } + + pub(crate) fn signature_threads(&self) -> usize { + self.signature_threads.unwrap_or_else(|| { + std::thread::available_parallelism() + .map(usize::from) + .map_err(|e| tracing::warn!("Failed to get parallelism, {e}")) + .unwrap_or(1) }) } diff --git a/src/main.rs b/src/main.rs index 1e635e2..e151937 100644 --- a/src/main.rs +++ b/src/main.rs @@ -259,7 +259,7 @@ async fn do_server_main( let keys = config.open_keys()?; - let spawner = Spawner::build()?; + let spawner = Spawner::build(config.signature_threads())?; let bind_address = config.bind_address(); let server = HttpServer::new(move || { diff --git a/src/requests.rs b/src/requests.rs index c8c4199..8883dba 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -461,12 +461,7 @@ pub(crate) struct Spawner { } impl Spawner { - pub(crate) fn build() -> std::io::Result { - let threads = std::thread::available_parallelism() - .map(usize::from) - .map_err(|e| tracing::warn!("Failed to get parallelism, {e}")) - .unwrap_or(1); - + pub(crate) fn build(threads: usize) -> std::io::Result { let (sender, receiver) = flume::bounded(8); let (shutdown, shutdown_rx) = flume::bounded(threads);