add --workers option

This commit is contained in:
2026-05-10 17:28:01 +02:00
parent bc07cba585
commit 6af66fd292
4 changed files with 23 additions and 24 deletions
Generated
-17
View File
@@ -481,7 +481,6 @@ dependencies = [
"ctrlc", "ctrlc",
"fancy-regex", "fancy-regex",
"num-integer", "num-integer",
"num_cpus",
"rpassword", "rpassword",
"sequoia-openpgp", "sequoia-openpgp",
] ]
@@ -605,12 +604,6 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.65" version = "0.1.65"
@@ -994,16 +987,6 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "num_cpus"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b"
dependencies = [
"hermit-abi",
"libc",
]
[[package]] [[package]]
name = "objc2" name = "objc2"
version = "0.6.4" version = "0.6.4"
-1
View File
@@ -15,6 +15,5 @@ clap = { version = "4.6.1", features = ["derive"] }
ctrlc = "3.5.2" ctrlc = "3.5.2"
fancy-regex = "0.18.0" fancy-regex = "0.18.0"
num-integer = "0.1.46" num-integer = "0.1.46"
num_cpus = "1.17.0"
rpassword = "7.5.2" rpassword = "7.5.2"
sequoia-openpgp = "2.2.0" sequoia-openpgp = "2.2.0"
+4 -3
View File
@@ -3,7 +3,7 @@
use std::{ use std::{
fmt::{self, Write}, fmt::{self, Write},
io, io,
num::NonZeroU64, num::NonZero,
sync::{ sync::{
atomic::{AtomicBool, AtomicU64, Ordering}, atomic::{AtomicBool, AtomicU64, Ordering},
mpsc, mpsc,
@@ -38,9 +38,10 @@ enum Message {
pub struct Config { pub struct Config {
pub regex: Regex, pub regex: Regex,
pub status_enabled: bool, pub status_enabled: bool,
pub stop_after: Option<NonZeroU64>, pub stop_after: Option<NonZero<u64>>,
pub password: Option<Password>, pub password: Option<Password>,
pub userids: Vec<UserID>, pub userids: Vec<UserID>,
pub workers: NonZero<usize>,
} }
#[derive(Debug)] #[derive(Debug)]
@@ -95,7 +96,7 @@ impl Fingerprunk {
None None
}; };
for num in 0..num_cpus::get() { for num in 0..self.config.workers.get() {
let sender = sender.clone(); let sender = sender.clone();
thread::Builder::new() thread::Builder::new()
+19 -3
View File
@@ -1,9 +1,9 @@
use std::{ use std::{
io::{self, IsTerminal}, io::{self, IsTerminal},
num::NonZeroU64, num::NonZero,
}; };
use anyhow::{Context as AnyhowContext, anyhow}; use anyhow::{Context, anyhow};
use clap::{ArgAction, Parser, ValueEnum}; use clap::{ArgAction, Parser, ValueEnum};
use fancy_regex::Regex; use fancy_regex::Regex;
use fingerprunk::Fingerprunk; use fingerprunk::Fingerprunk;
@@ -32,7 +32,7 @@ struct Args {
/// Stop once the specified number of matching keys has been found. /// Stop once the specified number of matching keys has been found.
#[arg(long)] #[arg(long)]
stop_after: Option<NonZeroU64>, stop_after: Option<NonZero<u64>>,
/// Prompt for a password and use it to encrypt matching keys. /// Prompt for a password and use it to encrypt matching keys.
/// ///
@@ -50,6 +50,13 @@ struct Args {
/// Disables the warning about importing keys without user IDs into GnuPG. /// Disables the warning about importing keys without user IDs into GnuPG.
#[arg(long, conflicts_with = "userid", action = ArgAction::SetTrue)] #[arg(long, conflicts_with = "userid", action = ArgAction::SetTrue)]
no_userid: bool, no_userid: bool,
/// Use the specified amount of worker threads.
///
/// If not specified, the amount of worker threads will be set to the amount of the machine's
/// available parallelism.
#[arg(long)]
workers: Option<NonZero<usize>>,
} }
#[derive(ValueEnum, Clone, Copy, Debug, Default)] #[derive(ValueEnum, Clone, Copy, Debug, Default)]
@@ -101,12 +108,21 @@ fn main() -> anyhow::Result<()> {
None None
}; };
let workers = match args.workers {
Some(workers) => workers,
None => std::thread::available_parallelism().context(
"unable to determine available parallelism, \
use --workers to specify amount of worker threads",
)?,
};
let config = fingerprunk::Config { let config = fingerprunk::Config {
regex: args.regex, regex: args.regex,
status_enabled: args.status.evaluate(), status_enabled: args.status.evaluate(),
stop_after: args.stop_after, stop_after: args.stop_after,
password, password,
userids: args.userid, userids: args.userid,
workers,
}; };
Fingerprunk::new_from_config(config).run()?; Fingerprunk::new_from_config(config).run()?;