Skip to content

Commit

Permalink
fix: avoid sending duplicate peer stats (#33)
Browse files Browse the repository at this point in the history
* avoid sending duplicate stats

* Adam was here

* fix

* Fix build

---------

Co-authored-by: Maciej Wójcik <[email protected]>
Co-authored-by: Adam Ciarciński <[email protected]>
  • Loading branch information
3 people authored Jul 25, 2023
1 parent 04aae14 commit 4f560f8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,28 @@ impl Gateway {
let userspace = self.config.userspace;
let stats_stream = async_stream::stream! {
let api = WGApi::new(ifname, userspace);
// helper map to track if peer data is actually changing
// and avoid sending duplicate stats
let mut peer_map = HashMap::new();
loop {
debug!("Sending peer stats update");
match api.read_host() {
Ok(host) => {
for peer in host
.peers
.values()
.into_values()
.filter(|p| p.last_handshake.map_or(
false,
|lhs| lhs != SystemTime::UNIX_EPOCH)
) {
yield peer.into();
let has_changed = match peer_map.get(&peer.public_key) {
Some(last_peer) => *last_peer != peer,
None => true,
};
if has_changed {
peer_map.insert(peer.public_key.clone(), peer.clone());
yield (&peer).into();
}
}
},
Err(err) => error!("Failed to retrieve WireGuard interface stats {}", err),
Expand Down
4 changes: 2 additions & 2 deletions src/wireguard/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use std::{
time::{Duration, SystemTime},
};

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Peer {
public_key: Key,
pub public_key: Key,
preshared_key: Option<Key>,
protocol_version: Option<u32>,
endpoint: Option<SocketAddr>,
Expand Down
2 changes: 1 addition & 1 deletion src/wireguard/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use netlink_packet_wireguard::{
};
use std::{error, fmt, net::IpAddr, str::FromStr};

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub struct IpAddrMask {
// IP v4 or v6
pub ip: IpAddr,
Expand Down

0 comments on commit 4f560f8

Please sign in to comment.