From b5e8d1fcd800330edfc4e8deed5229d172ec1248 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 27 Aug 2023 15:26:19 +0200 Subject: [PATCH] stdout: prettier formatting of proxy config --- src/main.rs | 28 ++++++++++++++++++++++++---- src/proxy_config.rs | 25 +++++++++++++++++++++---- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 43f3447..f84d973 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ extern crate anyhow; use log::*; +use std::collections::BTreeMap; use std::sync::Arc; use std::time::Instant; @@ -218,6 +219,8 @@ async fn dump_config_on_change( mut rx_proxy_config: watch::Receiver>, mut must_exit: watch::Receiver, ) { + let mut old_cfg: Arc = rx_proxy_config.borrow().clone(); + while !*must_exit.borrow() { select!( c = rx_proxy_config.changed() => { @@ -227,11 +230,28 @@ async fn dump_config_on_change( } _ = must_exit.changed() => continue, ); - println!("---- PROXY CONFIGURATION ----"); - for ent in rx_proxy_config.borrow().entries.iter() { - println!(" {}", ent); + + let cfg: Arc = rx_proxy_config.borrow().clone(); + if cfg != old_cfg { + let mut cfg_map = BTreeMap::<_, Vec<_>>::new(); + for ent in cfg.entries.iter() { + cfg_map + .entry((&ent.host, &ent.path_prefix)) + .or_default() + .push(ent); + } + + println!("---- PROXY CONFIGURATION ----"); + for ((host, prefix), ents) in cfg_map.iter_mut() { + println!("{}{}:", host, prefix.as_deref().unwrap_or_default()); + for ent in ents.iter() { + println!(" {}", ent); + } + } + println!(); + + old_cfg = cfg; } - println!(); } } diff --git a/src/proxy_config.rs b/src/proxy_config.rs index b4fab90..18f9d53 100644 --- a/src/proxy_config.rs +++ b/src/proxy_config.rs @@ -16,7 +16,7 @@ use crate::consul; // ---- Extract proxy config from Consul catalog ---- -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum HostDescription { Hostname(String), Pattern(glob::Pattern), @@ -43,7 +43,7 @@ impl std::fmt::Display for HostDescription { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { HostDescription::Hostname(h) => write!(f, "{}", h), - HostDescription::Pattern(p) => write!(f, "Pattern('{}')", p.as_str()), + HostDescription::Pattern(p) => write!(f, "[{}]", p.as_str()), } } } @@ -77,7 +77,21 @@ pub struct ProxyEntry { pub last_call: atomic::AtomicI64, } -#[derive(Debug, Clone, Copy)] +impl PartialEq for ProxyEntry { + fn eq(&self, other: &Self) -> bool { + self.host == other.host + && self.path_prefix == other.path_prefix + && self.priority == other.priority + && self.service_name == other.service_name + && self.target_addr == other.target_addr + && self.https_target == other.https_target + && self.flags == other.flags + && self.add_headers == other.add_headers + } +} +impl Eq for ProxyEntry {} + +#[derive(Debug, Clone, Copy, Eq, PartialEq)] pub struct ProxyEntryFlags { /// Is the target the same node as we are running on? /// (if yes priorize it over other matching targets) @@ -122,7 +136,7 @@ impl std::fmt::Display for ProxyEntry { } } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub struct ProxyConfig { pub entries: Vec, } @@ -383,6 +397,9 @@ pub fn spawn_proxy_config_task( entries.extend(parse_consul_catalog(catalog, same_node, same_site)); } } + + entries.sort_by_cached_key(|ent| ent.to_string()); + let config = ProxyConfig { entries }; tx.send(Arc::new(config)).expect("Internal error");