tricot/src/proxy_config.rs

272 lines
6.4 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
use std::net::SocketAddr;
2021-12-07 15:37:22 +00:00
use std::sync::{atomic, Arc};
use std::{cmp, time::Duration};
2021-12-07 16:56:15 +00:00
use anyhow::Result;
use futures::future::BoxFuture;
2021-12-07 16:56:15 +00:00
use futures::stream::{FuturesUnordered, StreamExt};
use log::*;
use tokio::{sync::watch, time::sleep};
use crate::consul::*;
// ---- Extract proxy config from Consul catalog ----
#[derive(Debug)]
pub enum HostDescription {
Hostname(String),
Pattern(glob::Pattern),
}
impl HostDescription {
fn new(desc: &str) -> Result<Self> {
if desc.chars().any(|x| matches!(x, '*' | '?' | '[' | ']')) {
Ok(Self::Pattern(glob::Pattern::new(desc)?))
} else {
Ok(Self::Hostname(desc.to_string()))
}
}
pub fn matches(&self, v: &str) -> bool {
match self {
Self::Pattern(p) => p.matches(v),
Self::Hostname(s) => s == v,
}
}
}
#[derive(Debug)]
pub struct ProxyEntry {
pub target_addr: SocketAddr,
2021-12-07 15:37:22 +00:00
pub host: HostDescription,
pub path_prefix: Option<String>,
pub priority: u32,
2021-12-07 17:19:51 +00:00
pub add_headers: Vec<(String, String)>,
2021-12-07 15:37:22 +00:00
// Counts the number of times this proxy server has been called to
// This implements a round-robin load balancer if there are multiple
// entries for the same host and same path prefix.
pub calls: atomic::AtomicU64,
}
2021-12-08 10:24:25 +00:00
impl std::fmt::Display for ProxyEntry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ", self.target_addr)?;
match &self.host {
HostDescription::Hostname(h) => write!(f, "{}", h)?,
HostDescription::Pattern(p) => write!(f, "Pattern('{}')", p.as_str())?,
}
write!(f, "{} {}", self.path_prefix.as_ref().unwrap_or(&String::new()), self.priority)?;
if !self.add_headers.is_empty() {
write!(f, "+Headers: {:?}", self.add_headers)?;
}
Ok(())
}
}
#[derive(Debug)]
pub struct ProxyConfig {
pub entries: Vec<ProxyEntry>,
}
fn retry_to_time(retries: u32, max_time: Duration) -> Duration {
// 1.2^x seems to be a good value to exponentially increase time at a good pace
// eg. 1.2^32 = 341 seconds ~= 5 minutes - ie. after 32 retries we wait 5
// minutes
return Duration::from_secs(cmp::min(
max_time.as_secs(),
1.2f64.powf(retries as f64) as u64,
));
}
fn parse_tricot_tag(
tag: &str,
target_addr: SocketAddr,
add_headers: &[(String, String)],
) -> Option<ProxyEntry> {
let splits = tag.split(' ').collect::<Vec<_>>();
if (splits.len() != 2 && splits.len() != 3) || splits[0] != "tricot" {
return None;
}
let (host, path_prefix) = match splits[1].split_once('/') {
Some((h, p)) => (h, Some(p.to_string())),
None => (splits[1], None),
};
let priority = match splits.len() {
3 => splits[2].parse().ok()?,
_ => 100,
};
let host = match HostDescription::new(host) {
Ok(h) => h,
Err(e) => {
warn!("Invalid hostname pattern {}: {}", host, e);
return None;
}
};
Some(ProxyEntry {
target_addr,
host,
path_prefix,
priority,
2021-12-07 17:19:51 +00:00
add_headers: add_headers.to_vec(),
2021-12-07 15:37:22 +00:00
calls: atomic::AtomicU64::from(0),
})
}
2021-12-07 17:19:51 +00:00
fn parse_tricot_add_header_tag(tag: &str) -> Option<(String, String)> {
let splits = tag.split(' ').collect::<Vec<_>>();
if splits.len() == 3 && splits[0] == "tricot-add-header" {
Some((splits[1].to_string(), splits[2].to_string()))
} else {
None
}
}
2021-12-07 16:56:15 +00:00
fn parse_consul_catalog(catalog: &ConsulNodeCatalog) -> Vec<ProxyEntry> {
let mut entries = vec![];
for (_, svc) in catalog.services.iter() {
let ip_addr = match svc.address.parse() {
Ok(ip) => ip,
_ => match catalog.node.address.parse() {
Ok(ip) => ip,
_ => {
warn!(
"Could not get address for service {} at node {}",
svc.service, catalog.node.node
);
continue;
}
},
};
let addr = SocketAddr::new(ip_addr, svc.port);
2021-12-07 17:19:51 +00:00
let mut add_headers = vec![];
for tag in svc.tags.iter() {
if let Some(pair) = parse_tricot_add_header_tag(tag) {
add_headers.push(pair);
}
}
for tag in svc.tags.iter() {
2021-12-07 17:19:51 +00:00
if let Some(ent) = parse_tricot_tag(tag, addr, &add_headers[..]) {
entries.push(ent);
}
}
}
2021-12-07 16:56:15 +00:00
entries
}
2021-12-07 16:56:15 +00:00
#[derive(Default)]
struct NodeWatchState {
last_idx: Option<usize>,
last_catalog: Option<ConsulNodeCatalog>,
retries: u32,
}
pub fn spawn_proxy_config_task(consul: Consul) -> watch::Receiver<Arc<ProxyConfig>> {
2021-12-07 12:50:44 +00:00
let (tx, rx) = watch::channel(Arc::new(ProxyConfig {
entries: Vec::new(),
2021-12-07 12:50:44 +00:00
}));
2021-12-07 16:56:15 +00:00
let consul = Arc::new(consul);
tokio::spawn(async move {
2021-12-07 16:56:15 +00:00
let mut nodes = HashMap::new();
let mut watches = FuturesUnordered::<BoxFuture<'static, (String, Result<_>)>>::new();
loop {
2021-12-07 16:56:15 +00:00
match consul.list_nodes().await {
Ok(consul_nodes) => {
info!("Watched consul nodes: {:?}", consul_nodes);
for node in consul_nodes {
if !nodes.contains_key(&node) {
nodes.insert(node.clone(), NodeWatchState::default());
let node = node.to_string();
let consul = consul.clone();
watches.push(Box::pin(async move {
let res = consul.watch_node(&node, None).await;
(node, res)
}));
}
}
}
Err(e) => {
warn!("Could not get Consul node list: {}", e);
}
}
let (node, res): (String, Result<_>) = match watches.next().await {
Some(v) => v,
None => {
warn!("No nodes currently watched in proxy_config.rs");
sleep(Duration::from_secs(10)).await;
continue;
}
};
match res {
Ok((catalog, new_idx)) => {
let mut watch_state = nodes.get_mut(&node).unwrap();
watch_state.last_idx = Some(new_idx);
watch_state.last_catalog = Some(catalog);
watch_state.retries = 0;
let idx = watch_state.last_idx;
let consul = consul.clone();
watches.push(Box::pin(async move {
let res = consul.watch_node(&node, idx).await;
(node, res)
}));
}
Err(e) => {
2021-12-07 16:56:15 +00:00
let mut watch_state = nodes.get_mut(&node).unwrap();
watch_state.retries += 1;
watch_state.last_idx = None;
let will_retry_in =
retry_to_time(watch_state.retries, Duration::from_secs(600));
error!(
"Failed to query consul for node {}. Will retry in {}s. {}",
node,
will_retry_in.as_secs(),
e
);
2021-12-07 16:56:15 +00:00
let consul = consul.clone();
watches.push(Box::pin(async move {
sleep(will_retry_in).await;
let res = consul.watch_node(&node, None).await;
(node, res)
}));
continue;
}
2021-12-07 16:56:15 +00:00
}
2021-12-07 16:56:15 +00:00
let mut entries = vec![];
for (_, watch_state) in nodes.iter() {
if let Some(catalog) = &watch_state.last_catalog {
entries.extend(parse_consul_catalog(catalog));
}
}
let config = ProxyConfig { entries };
2021-12-07 12:50:44 +00:00
tx.send(Arc::new(config)).expect("Internal error");
}
});
rx
}