From fc3cea4e6707f676072f9acacea9caed71f16592 Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 25 Feb 2021 14:12:36 +0800 Subject: [PATCH] bin: allow specifying the number of runtime worker threads --- leaf-bin/Cargo.toml | 4 +--- leaf-bin/src/main.rs | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/leaf-bin/Cargo.toml b/leaf-bin/Cargo.toml index dc62ca0..112264b 100644 --- a/leaf-bin/Cargo.toml +++ b/leaf-bin/Cargo.toml @@ -27,12 +27,10 @@ default-openssl = [ "leaf/openssl-tls", ] -multi-thread = ["tokio/rt-threaded"] - [dependencies] leaf = { path = "../leaf", default-features = false, optional = true } clap = { version = "3.0.0-beta.1", default-features = false, features =["std"] } log = { version = "0.4", features = ["std"] } fern = { version = "0.5", features = ["colored"] } -tokio = { version = "0.2", features = ["signal"] } +tokio = { version = "0.2", features = ["signal", "rt-threaded"] } futures = "0.3" diff --git a/leaf-bin/src/main.rs b/leaf-bin/src/main.rs index b148eef..1001aac 100644 --- a/leaf-bin/src/main.rs +++ b/leaf-bin/src/main.rs @@ -30,6 +30,14 @@ fn main() { .takes_value(true) .default_value("config.conf"), ) + .arg( + Arg::new("threads") + .long("threads") + .value_name("N") + .about("Specifies the number of runtime threads. Note that the default Tokio runtime worker thread stack size is 2MB.") + .takes_value(true) + .default_value("auto"), + ) .arg( Arg::new("test-outbound") .short('t') @@ -51,21 +59,33 @@ fn main() { }; let mut rt = { - #[cfg(feature = "multi-thread")] - { + let threads = matches.value_of("threads").unwrap(); + if threads == "auto" { tokio::runtime::Builder::new() .threaded_scheduler() .enable_all() .build() .unwrap() - } - #[cfg(not(feature = "multi-thread"))] - { - tokio::runtime::Builder::new() - .basic_scheduler() - .enable_all() - .build() - .unwrap() + } else { + if let Ok(n) = threads.parse::() { + if n > 1 { + tokio::runtime::Builder::new() + .threaded_scheduler() + .core_threads(n) + .enable_all() + .build() + .unwrap() + } else { + tokio::runtime::Builder::new() + .basic_scheduler() + .enable_all() + .build() + .unwrap() + } + } else { + println!("invalid number of threads"); + exit(1); + } } };