From 44c763b67e8c4cad00359c9f660b696cc92ee4c9 Mon Sep 17 00:00:00 2001 From: Martin Hoffmann Date: Wed, 24 Jan 2024 17:12:40 +0100 Subject: [PATCH] Replace `num_cpus` with `std::thread::available_parallelism`. (#912) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR removes the dependency on the num_cpus crate by using the standard library’s std::thread::available_parallelism instead. --- Cargo.lock | 1 - Cargo.toml | 1 - src/config.rs | 25 ++++++++++++++++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b9db0ca..04f2dda1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1150,7 +1150,6 @@ dependencies = [ "listenfd", "log", "nix", - "num_cpus", "pin-project-lite", "rand", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index b46dcf55..705fb7e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,6 @@ futures = "0.3.4" hyper = { version = "0.14", features = [ "server", "stream" ] } listenfd = "1" log = "0.4.8" -num_cpus = "1.12.0" pin-project-lite = "0.2.4" rand = "0.8.1" reqwest = { version = "0.11.0", default-features = false, features = ["blocking", "rustls-tls" ] } diff --git a/src/config.rs b/src/config.rs index bb8713fe..41a4381f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ use std::io::Read; use std::net::{IpAddr, SocketAddr}; use std::path::{Path, PathBuf}; use std::str::FromStr; +use std::thread::available_parallelism; use std::time::Duration; use clap::{ Command, Args, ArgAction, ArgMatches, FromArgMatches, Parser, @@ -955,8 +956,11 @@ impl Config { dirty_repository: file.take_bool("dirty")?.unwrap_or(false), validation_threads: { - file.take_small_usize("validation-threads")? - .unwrap_or_else(::num_cpus::get) + file.take_small_usize( + "validation-threads" + )?.unwrap_or_else(|| { + Config::default_validation_threads() + }) }, refresh: { Duration::from_secs( @@ -1155,7 +1159,7 @@ impl Config { enable_bgpsec: false, enable_aspa: false, dirty_repository: DEFAULT_DIRTY_REPOSITORY, - validation_threads: ::num_cpus::get(), + validation_threads: Config::default_validation_threads(), refresh: Duration::from_secs(DEFAULT_REFRESH), retry: Duration::from_secs(DEFAULT_RETRY), expire: Duration::from_secs(DEFAULT_EXPIRE), @@ -1182,6 +1186,11 @@ impl Config { } } + /// Returns the default value for validation threads. + fn default_validation_threads() -> usize { + available_parallelism().map(|x| x.get()).unwrap_or(1) + } + /// Alters paths so that they are relative to a possible chroot. pub fn adjust_chroot_paths(&mut self) -> Result<(), Failed> { if let Some(ref chroot) = self.chroot { @@ -2590,7 +2599,10 @@ mod test { assert!(config.extra_tals_dir.is_none()); assert!(config.exceptions.is_empty()); assert_eq!(config.strict, DEFAULT_STRICT); - assert_eq!(config.validation_threads, ::num_cpus::get()); + assert_eq!( + config.validation_threads, + Config::default_validation_threads(), + ); assert_eq!(config.refresh, Duration::from_secs(DEFAULT_REFRESH)); assert_eq!(config.retry, Duration::from_secs(DEFAULT_RETRY)); assert_eq!(config.expire, Duration::from_secs(DEFAULT_EXPIRE)); @@ -2673,7 +2685,10 @@ mod test { ); assert!(config.exceptions.is_empty()); assert!(!config.strict); - assert_eq!(config.validation_threads, ::num_cpus::get()); + assert_eq!( + config.validation_threads, + Config::default_validation_threads() + ); assert_eq!(config.refresh, Duration::from_secs(DEFAULT_REFRESH)); assert_eq!(config.retry, Duration::from_secs(DEFAULT_RETRY)); assert_eq!(config.expire, Duration::from_secs(DEFAULT_EXPIRE));