Skip to content

Commit f30876c

Browse files
fix(netwatch): add last_updated to State so time jumps always notify watchers
1 parent f199ee2 commit f30876c

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

netwatch/src/interfaces.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ pub struct State {
188188
///
189189
/// When set, its value is the map key into `interface` and `interface_ips`.
190190
pub default_route_interface: Option<String>,
191+
192+
/// Monotonic timestamp used to force [`n0_watcher::Watchable`] notifications.
193+
pub last_updated: n0_future::time::Instant,
191194
}
192195

193196
impl fmt::Display for State {
@@ -251,6 +254,7 @@ impl State {
251254
have_v6,
252255
is_expensive: false,
253256
default_route_interface,
257+
last_updated: n0_future::time::Instant::now(),
254258
}
255259
}
256260

@@ -267,9 +271,20 @@ impl State {
267271
have_v4: true,
268272
is_expensive: false,
269273
default_route_interface: Some(ifname),
274+
last_updated: n0_future::time::Instant::now(),
270275
}
271276
}
272277

278+
/// Compares network state ignoring `last_updated`.
279+
pub fn eq_ignoring_timestamp(&self, other: &State) -> bool {
280+
self.interfaces == other.interfaces
281+
&& self.local_addresses == other.local_addresses
282+
&& self.have_v6 == other.have_v6
283+
&& self.have_v4 == other.have_v4
284+
&& self.is_expensive == other.is_expensive
285+
&& self.default_route_interface == other.default_route_interface
286+
}
287+
273288
/// Is this a major change compared to the `old` one?.
274289
pub fn is_major_change(&self, old: &State) -> bool {
275290
if self.have_v6 != old.have_v6

netwatch/src/interfaces/wasm_browser.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{collections::HashMap, fmt};
22

33
use js_sys::{JsString, Reflect};
4+
use n0_future::time::Instant;
45

56
pub const BROWSER_INTERFACE: &str = "browserif";
67

@@ -73,6 +74,9 @@ pub struct State {
7374

7475
/// The URL to the Proxy Autoconfig URL, if applicable.
7576
pub(crate) pac: Option<String>,
77+
78+
/// Monotonic timestamp used to force [`n0_watcher::Watchable`] notifications.
79+
pub last_updated: Instant,
7680
}
7781

7882
impl fmt::Display for State {
@@ -113,9 +117,21 @@ impl State {
113117
default_route_interface: Some(BROWSER_INTERFACE.to_string()),
114118
http_proxy: None,
115119
pac: None,
120+
last_updated: Instant::now(),
116121
}
117122
}
118123

124+
/// Compares network state ignoring `last_updated`.
125+
pub fn eq_ignoring_timestamp(&self, other: &State) -> bool {
126+
self.interfaces == other.interfaces
127+
&& self.have_v6 == other.have_v6
128+
&& self.have_v4 == other.have_v4
129+
&& self.is_expensive == other.is_expensive
130+
&& self.default_route_interface == other.default_route_interface
131+
&& self.http_proxy == other.http_proxy
132+
&& self.pac == other.pac
133+
}
134+
119135
/// Is this a major change compared to the `old` one?.
120136
pub fn is_major_change(&self, old: &State) -> bool {
121137
// All changes are major.

netwatch/src/netmon/actor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl Actor {
147147
let old_state = &self.interface_state.get();
148148

149149
// No major changes, continue on
150-
if !time_jumped && old_state == &new_state {
150+
if !time_jumped && old_state.eq_ignoring_timestamp(&new_state) {
151151
debug!("no changes detected");
152152
return;
153153
}

0 commit comments

Comments
 (0)