stdout: prettier formatting of proxy config

This commit is contained in:
Alex 2023-08-27 15:26:19 +02:00
parent 48a421a3b6
commit b5e8d1fcd8
2 changed files with 45 additions and 8 deletions

View file

@ -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,
);
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 ent in rx_proxy_config.borrow().entries.iter() {
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;
}
}
}

View file

@ -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");