Skip to content

Commit

Permalink
nydusd: wrap thread validator
Browse files Browse the repository at this point in the history
Signed-off-by: Yan Song <yansong.ys@antfin.com>
  • Loading branch information
imeoer committed Jun 28, 2022
1 parent aef5cef commit 30fa3f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
27 changes: 3 additions & 24 deletions src/bin/nydusd/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::api_server_glue::ApiServerController;
use crate::blob_cache::BlobCacheMgr;
use crate::daemon::{DaemonError, NydusDaemon};
use crate::fs_service::{FsBackendMountCmd, FsService};
use crate::service_controller::create_daemon;
use crate::service_controller::{create_daemon, thread_validator};

#[cfg(feature = "fusedev")]
mod fusedev;
Expand Down Expand Up @@ -270,17 +270,7 @@ fn append_fuse_options(app: App<'static, 'static>) -> App<'static, 'static> {
.help("Number of worker threads to serve IO requests")
.takes_value(true)
.required(false)
.validator(|v| {
if let Ok(t) = v.parse::<i32>() {
if t > 0 && t <= 1024 {
Ok(())
} else {
Err("Invalid working thread number {}, valid values: [1-1024]".to_string())
}
} else {
Err("Input thread number is invalid".to_string())
}
}),
.validator(thread_validator),
)
.arg(
Arg::with_name("writable")
Expand Down Expand Up @@ -351,18 +341,7 @@ fn append_services_subcmd_options(app: App<'static, 'static>) -> App<'static, 's
.help("Number of working threads to serve fscache requests")
.takes_value(true)
.required(false)
.validator(|v| {
if let Ok(t) = v.parse::<i32>() {
if t > 0 && t <= 1024 {
Ok(())
} else {
Err("Invalid working thread number {}, valid values: [1-1024]"
.to_string())
}
} else {
Err("Input thread number is invalid".to_string())
}
}),
.validator(thread_validator),
);

app.subcommand(subcmd)
Expand Down
35 changes: 21 additions & 14 deletions src/bin/nydusd/service_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ pub struct ServiceController {
fscache: Mutex<Option<Arc<crate::fs_cache::FsCacheHandler>>>,
}

pub fn thread_validator(v: String) -> std::result::Result<(), String> {
ensure_thread(v).map(|_| ())
}

fn ensure_thread<V: AsRef<str>>(v: V) -> std::result::Result<i32, String> {
if let Ok(t) = v.as_ref().parse::<i32>() {
if t > 0 && t <= 1024 {
Ok(t)
} else {
Err("Invalid working thread number {}, valid values: [1-1024]".to_string())
}
} else {
Err("Input thread number is invalid".to_string())
}
}

impl ServiceController {
/// Start all enabled services.
fn start_services(&self) -> Result<()> {
Expand Down Expand Up @@ -120,20 +136,11 @@ impl ServiceController {
};
let tag = subargs.value_of("fscache-tag");

let mut threads = 1usize;
if let Some(threads_value) = subargs.value_of("fscache-threads") {
if let Ok(t) = threads_value.parse::<i32>() {
if t > 0 && t <= 1024 {
threads = t as usize;
} else {
return Err(einval!(
"Invalid working thread number {}, valid values: [1-1024]"
));
}
} else {
return Err(einval!("Input thread number is invalid".to_string()));
}
}
let threads = if let Some(threads_value) = subargs.value_of("fscache-threads") {
ensure_thread(threads_value).map_err(|err| einval!(err))? as usize
} else {
1usize
};

info!(
"Create fscache instance at {} with tag {}, {} working threads",
Expand Down

0 comments on commit 30fa3f6

Please sign in to comment.