Skip to content

Commit 2ba8294

Browse files
authored
Fix stats sending loop (#148)
* Update gateway.rs * don't hold lock in match * Update gateway.rs * bump version
1 parent e6fcab7 commit 2ba8294

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "defguard-gateway"
3-
version = "1.2.0"
3+
version = "1.2.1"
44
edition = "2021"
55

66
[dependencies]

src/gateway.rs

+24-29
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,11 @@ impl Gateway {
163163
}
164164

165165
// check if all IPs are the same
166-
!new_peers
167-
.iter()
168-
.all(|peer| match self.peers.get(&peer.pubkey) {
169-
Some(p) => peer.allowed_ips == p.allowed_ips,
170-
None => false,
171-
})
166+
!new_peers.iter().all(|peer| {
167+
self.peers
168+
.get(&peer.pubkey)
169+
.map_or(false, |p| peer.allowed_ips == p.allowed_ips)
170+
})
172171
}
173172

174173
/// Starts tokio thread collecting stats and sending them to backend service via gRPC.
@@ -188,13 +187,12 @@ impl Gateway {
188187
let mut peer_map = HashMap::new();
189188
let mut interval = interval(period);
190189
let mut id = 1;
191-
loop {
190+
'outer: loop {
192191
// wait until next iteration
193192
interval.tick().await;
194-
let mut payload = Payload::Empty(());
195-
196-
debug!("Sending active peer stats update.");
197-
match wgapi.lock().unwrap().read_interface_data() {
193+
debug!("Sending active peer stats updates.");
194+
let interface_data = wgapi.lock().unwrap().read_interface_data();
195+
match interface_data {
198196
Ok(host) => {
199197
let peers = host.peers;
200198
debug!(
@@ -205,13 +203,22 @@ impl Gateway {
205203
p.last_handshake
206204
.map_or(false, |lhs| lhs != SystemTime::UNIX_EPOCH)
207205
}) {
208-
let has_changed = match peer_map.get(&peer.public_key) {
209-
Some(last_peer) => *last_peer != peer,
210-
None => true,
211-
};
206+
let has_changed = peer_map
207+
.get(&peer.public_key)
208+
.map_or(true, |last_peer| *last_peer != peer);
212209
if has_changed {
213210
peer_map.insert(peer.public_key.clone(), peer.clone());
214-
payload = Payload::PeerStats((&peer).into());
211+
id += 1;
212+
if tx
213+
.send(StatsUpdate {
214+
id,
215+
payload: Some(Payload::PeerStats((&peer).into())),
216+
})
217+
.is_err()
218+
{
219+
debug!("Stats stream disappeared");
220+
break 'outer;
221+
}
215222
} else {
216223
debug!(
217224
"Stats for peer {} have not changed. Skipping.",
@@ -222,19 +229,7 @@ impl Gateway {
222229
}
223230
Err(err) => error!("Failed to retrieve WireGuard interface stats: {err}"),
224231
}
225-
226-
id += 1;
227-
if tx
228-
.send(StatsUpdate {
229-
id,
230-
payload: Some(payload),
231-
})
232-
.is_err()
233-
{
234-
debug!("Stats stream disappeared");
235-
break;
236-
}
237-
debug!("Active peer stats update sent.");
232+
debug!("Sent peer stats updates for all peers.");
238233
}
239234
});
240235
self.stats_thread = Some(handle);

0 commit comments

Comments
 (0)