From 49d94dae1d753c1f3349be7ea9bc7e7978c0af15 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 21 Apr 2023 14:14:07 +0200 Subject: [PATCH] less noise --- src/autodiscovery.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/autodiscovery.rs b/src/autodiscovery.rs index 7b61da6..86276c1 100644 --- a/src/autodiscovery.rs +++ b/src/autodiscovery.rs @@ -13,13 +13,13 @@ use tracing::*; use df_consul::*; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] pub struct DiplonatAutodiscoveryResult { pub timestamp: u64, pub address: Option, } -#[derive(Default, Debug)] +#[derive(Default, Debug, Eq, PartialEq)] pub struct AutodiscoveredAddresses { pub ipv4: HashMap>, pub ipv6: HashMap>, @@ -30,6 +30,7 @@ pub fn watch_autodiscovered_ips( mut must_exit: watch::Receiver, ) -> watch::Receiver> { let (tx, rx) = watch::channel(Arc::new(AutodiscoveredAddresses::default())); + let rx2 = rx.clone(); tokio::spawn(async move { let mut last_index = None; @@ -66,9 +67,13 @@ pub fn watch_autodiscovered_ips( } } - debug!("Autodiscovered addresses fetched from Consul: {:?}", addresses); + if addresses.strip_timestamps() != rx2.borrow().strip_timestamps() { + addresses.dump(); + } + if tx.send(Arc::new(addresses)).is_err() { info!("Autodiscovered addresses watcher terminating"); + return; } } }); @@ -103,6 +108,24 @@ fn parse_autodiscovered_address( Ok(()) } +impl AutodiscoveredAddresses { + fn strip_timestamps(&self) -> (HashMap<&str, Option>, HashMap<&str, Option>) { + (self.ipv4.iter().map(|(k, v)| (k.as_str(), v.address)).collect(), + self.ipv6.iter().map(|(k, v)| (k.as_str(), v.address)).collect()) + } + + fn dump(&self) { + println!("---- Autodiscovered addresses (fetched from DiploNAT): ----"); + for (k, v) in self.ipv4.iter() { + println!(" IPv4 {} {} {:?}", k, v.timestamp, v.address); + } + for (k, v) in self.ipv6.iter() { + println!(" IPv6 {} {} {:?}", k, v.timestamp, v.address); + } + println!(""); + } +} + pub fn timestamp() -> u64 { SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH)