From acd724e4f5ac6693e9583f1661b0ce0240c73843 Mon Sep 17 00:00:00 2001 From: Borber Date: Wed, 15 Jul 2026 01:22:05 +0800 Subject: [PATCH] feat: make netwatcher optional --- dquic/Cargo.toml | 1 + qconnection/Cargo.toml | 1 + qinterface/Cargo.toml | 3 ++- qinterface/src/device.rs | 12 ++++++++++-- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/dquic/Cargo.toml b/dquic/Cargo.toml index 38fe42cc..5ea8f588 100644 --- a/dquic/Cargo.toml +++ b/dquic/Cargo.toml @@ -44,6 +44,7 @@ tracing-appender = { workspace = true } default = ["datagram"] telemetry = ["qconnection/telemetry"] datagram = ["qconnection/datagram"] +netwatcher = ["qconnection/netwatcher"] [dev-dependencies.tracing-subscriber] workspace = true diff --git a/qconnection/Cargo.toml b/qconnection/Cargo.toml index 9bfedfe0..d7c05263 100644 --- a/qconnection/Cargo.toml +++ b/qconnection/Cargo.toml @@ -47,6 +47,7 @@ qdatagram = { workspace = true, optional = true } [features] default = ["datagram"] datagram = ["dep:qdatagram"] +netwatcher = ["qinterface/netwatcher"] telemetry = ["qevent/telemetry"] [dev-dependencies] diff --git a/qinterface/Cargo.toml b/qinterface/Cargo.toml index f97998bf..74333329 100644 --- a/qinterface/Cargo.toml +++ b/qinterface/Cargo.toml @@ -16,7 +16,7 @@ derive_more = { workspace = true, features = ["deref"] } futures = { workspace = true } http = { workspace = true } netdev = { workspace = true } -netwatcher = { workspace = true } +netwatcher = { workspace = true, optional = true } parking_lot = { workspace = true } qbase = { workspace = true } qevent = { workspace = true } @@ -37,4 +37,5 @@ tokio = { workspace = true, features = [ ] } [features] +netwatcher = ["dep:netwatcher"] qudp = ["dep:qudp"] diff --git a/qinterface/src/device.rs b/qinterface/src/device.rs index 981dd0b1..d566107b 100644 --- a/qinterface/src/device.rs +++ b/qinterface/src/device.rs @@ -1,14 +1,18 @@ +#[cfg(feature = "netwatcher")] +use std::sync::Mutex; use std::{ collections::HashMap, fmt::Debug, net::IpAddr, - sync::{Arc, Mutex, OnceLock, RwLock}, + sync::{Arc, OnceLock, RwLock}, time::Duration, }; use derive_more::{Deref, DerefMut}; pub use netdev::Interface; +#[cfg(feature = "netwatcher")] pub use netwatcher::Error as WatcherError; +#[cfg(feature = "netwatcher")] use netwatcher::WatchHandle; use qbase::{ net::Family, @@ -321,6 +325,7 @@ impl State { pub struct Devices { state: Arc, + #[cfg(feature = "netwatcher")] watcher: Mutex>, _timer: AbortOnDropHandle<()>, } @@ -329,7 +334,6 @@ impl Debug for Devices { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Devices") .field("state", &self.state) - .field("watcher", &"...") .field("_timer", &self._timer) .finish() } @@ -357,6 +361,7 @@ impl Devices { .in_current_span() })); + #[cfg(feature = "netwatcher")] let watcher = netwatcher::watch_interfaces_with_callback({ let state = state.clone(); move |_update| { @@ -365,6 +370,7 @@ impl Devices { } }); + #[cfg(feature = "netwatcher")] if let Err(initial_watcher_error) = &watcher { tracing::warn!(target: "interface", "failed to start interfaces watcher: {initial_watcher_error}"); } @@ -372,10 +378,12 @@ impl Devices { Self { state, _timer: timer, + #[cfg(feature = "netwatcher")] watcher: watcher.into(), } } + #[cfg(feature = "netwatcher")] #[inline] pub fn restart_watcher(&self) -> Result<(), WatcherError> { let new_watcher = netwatcher::watch_interfaces_with_callback({