diff options
author | Alex Auvolat <alex@adnab.me> | 2023-08-27 15:26:19 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2023-08-27 15:26:19 +0200 |
commit | b5e8d1fcd800330edfc4e8deed5229d172ec1248 (patch) | |
tree | e1377e66ac3f16cd6c5136aa0933da9433b3e48e | |
parent | 48a421a3b6c07705b67276853643ed5601a30154 (diff) | |
download | tricot-b5e8d1fcd800330edfc4e8deed5229d172ec1248.tar.gz tricot-b5e8d1fcd800330edfc4e8deed5229d172ec1248.zip |
stdout: prettier formatting of proxy config
-rw-r--r-- | src/main.rs | 28 | ||||
-rw-r--r-- | 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<Arc<ProxyConfig>>, mut must_exit: watch::Receiver<bool>, ) { + let mut old_cfg: Arc<ProxyConfig> = 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<ProxyConfig> = 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<ProxyEntry>, } @@ -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"); |