Skip to content

Commit

Permalink
Replace num_cpus with std::thread::available_parallelism. (#912)
Browse files Browse the repository at this point in the history
This PR removes the dependency on the num_cpus crate by using the standard
library’s std::thread::available_parallelism instead.
  • Loading branch information
partim authored Jan 24, 2024
1 parent b92ee96 commit 44c763b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
Expand Down
25 changes: 20 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 44c763b

Please sign in to comment.