From be000c20469cacdf6a88a65389a8089f3693370e Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 22 Sep 2022 09:30:37 -0400 Subject: [PATCH] Centralize import of tokio to control its version and features. --- controller/Cargo.toml | 3 +-- controller/src/controller.rs | 14 +++++++++----- utils/Cargo.toml | 6 +++++- utils/src/lib.rs | 7 ++++++- utils/src/reaper.rs | 4 ++-- vl1-service/src/vl1service.rs | 2 +- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/controller/Cargo.toml b/controller/Cargo.toml index d01579c6f..a0093af8e 100644 --- a/controller/Cargo.toml +++ b/controller/Cargo.toml @@ -9,11 +9,10 @@ path = "src/main.rs" [dependencies] zerotier-crypto = { path = "../crypto" } -zerotier-utils = { path = "../utils" } +zerotier-utils = { path = "../utils", features = ["tokio"] } zerotier-network-hypervisor = { path = "../network-hypervisor" } zerotier-vl1-service = { path = "../vl1-service" } async-trait = "^0" -tokio = { version = "^1", features = ["fs", "io-util", "io-std", "net", "parking_lot", "process", "rt", "rt-multi-thread", "signal", "sync", "time"], default-features = false } parking_lot = { version = "^0", features = [], default-features = false } serde = { version = "^1", features = ["derive"], default-features = false } serde_json = { version = "^1", features = ["std"], default-features = false } diff --git a/controller/src/controller.rs b/controller/src/controller.rs index 5cc453834..ee10978a5 100644 --- a/controller/src/controller.rs +++ b/controller/src/controller.rs @@ -1,8 +1,9 @@ -use crate::database::Database; +// (c) 2020-2022 ZeroTier, Inc. -- currently propritery pending actual release and licensing. See LICENSE.md. use std::sync::Arc; use tokio::time::{Duration, Instant}; +use zerotier_utils::tokio; use zerotier_network_hypervisor::protocol::{verbs, PacketBuffer}; use zerotier_network_hypervisor::util::dictionary::Dictionary; @@ -11,16 +12,19 @@ use zerotier_network_hypervisor::vl2::NetworkId; use zerotier_utils::reaper::Reaper; +use crate::database::Database; + const REQUEST_TIMEOUT: Duration = Duration::from_secs(10); pub struct Controller { database: Arc, reaper: Reaper, + runtime: tokio::runtime::Handle, } impl Controller { - pub async fn new(database: Arc) -> Arc { - Arc::new(Self { database, reaper: Reaper::new() }) + pub async fn new(database: Arc, runtime: tokio::runtime::Handle) -> Arc { + Arc::new(Self { database, reaper: Reaper::new(&runtime), runtime }) } async fn handle_network_config_request( @@ -86,7 +90,7 @@ impl InnerProtocol for Controller { if let Some(deadline) = Instant::now().checked_add(REQUEST_TIMEOUT) { self.reaper.add( - tokio::spawn(Self::handle_network_config_request( + self.runtime.spawn(Self::handle_network_config_request( self.database.clone(), source.clone(), source_path.clone(), @@ -98,7 +102,7 @@ impl InnerProtocol for Controller { deadline, ); } else { - eprintln!("WARNING: instant + REQUEST_TIMEOUT overflowed! should be impossible."); + eprintln!("WARNING: Instant::now() + REQUEST_TIMEOUT overflowed! should be impossible."); } PacketHandlerResult::Ok diff --git a/utils/Cargo.toml b/utils/Cargo.toml index be97870d6..fa69621be 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -5,8 +5,12 @@ license = "MPL-2.0" name = "zerotier-utils" version = "0.1.0" +[features] +default = [] +tokio = ["dep:tokio"] + [dependencies] parking_lot = { version = "^0", features = [], default-features = false } serde = { version = "^1", features = ["derive"], default-features = false } serde_json = { version = "^1", features = ["std"], default-features = false } -tokio = { version = "^1", features = ["fs", "io-util", "io-std", "net", "parking_lot", "process", "rt", "rt-multi-thread", "signal", "sync", "time"], default-features = false } +tokio = { version = "^1", default-features = false, features = ["fs", "io-util", "io-std", "net", "parking_lot", "process", "rt", "rt-multi-thread", "signal", "sync", "time"], optional = true } diff --git a/utils/src/lib.rs b/utils/src/lib.rs index d34a92efe..1aee6ec9e 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -10,11 +10,16 @@ pub mod hex; pub mod json; pub mod memory; pub mod pool; -pub mod reaper; pub mod ringbuffer; pub mod ringbuffermap; pub mod varint; +#[cfg(feature = "tokio")] +pub mod reaper; + +#[cfg(feature = "tokio")] +pub use tokio; + /// Get milliseconds since unix epoch. pub fn ms_since_epoch() -> i64 { std::time::SystemTime::now() diff --git a/utils/src/reaper.rs b/utils/src/reaper.rs index 71f4b3b58..a197cb5c0 100644 --- a/utils/src/reaper.rs +++ b/utils/src/reaper.rs @@ -12,11 +12,11 @@ pub struct Reaper { } impl Reaper { - pub fn new() -> Self { + pub fn new(runtime: &tokio::runtime::Handle) -> Self { let q = Arc::new((parking_lot::Mutex::new(VecDeque::with_capacity(16)), Notify::new())); Self { q: q.clone(), - finisher: tokio::spawn(async move { + finisher: runtime.spawn(async move { loop { q.1.notified().await; loop { diff --git a/vl1-service/src/vl1service.rs b/vl1-service/src/vl1service.rs index 1f0cfa4f9..2d12e2c54 100644 --- a/vl1-service/src/vl1service.rs +++ b/vl1-service/src/vl1service.rs @@ -315,7 +315,7 @@ impl> = state.daemons.drain(..).collect(); drop(state); for d in daemons.drain(..) { - d.join(); + let _ = d.join(); } } }