Don't do the hack with same_site and same_node, separate lb flags

This commit is contained in:
Alex 2023-01-02 17:33:05 +01:00
parent 3c5aa3680e
commit f163d1d348
Signed by untrusted user: lx
GPG key ID: 0E496D15096376BE
2 changed files with 42 additions and 22 deletions

View file

@ -247,8 +247,8 @@ async fn select_target_and_proxy(
.as_ref() .as_ref()
.map(|x| x.len() as i32) .map(|x| x.len() as i32)
.unwrap_or(0), .unwrap_or(0),
ent.same_node, (ent.flags.same_node || ent.flags.site_lb || ent.flags.global_lb),
ent.same_site, (ent.flags.same_site || ent.flags.global_lb),
-ent.calls_in_progress.load(Ordering::SeqCst), -ent.calls_in_progress.load(Ordering::SeqCst),
-ent.last_call.load(Ordering::SeqCst), -ent.last_call.load(Ordering::SeqCst),
) )
@ -260,8 +260,8 @@ async fn select_target_and_proxy(
"target_addr", "target_addr",
proxy_to.target_addr.to_string(), proxy_to.target_addr.to_string(),
)); ));
tags.push(KeyValue::new("same_node", proxy_to.same_node)); tags.push(KeyValue::new("same_node", proxy_to.flags.same_node));
tags.push(KeyValue::new("same_site", proxy_to.same_site)); tags.push(KeyValue::new("same_site", proxy_to.flags.same_site));
proxy_to.last_call.fetch_max( proxy_to.last_call.fetch_max(
(received_time - https_config.time_origin).as_millis() as i64, (received_time - https_config.time_origin).as_millis() as i64,

View file

@ -64,12 +64,8 @@ pub struct ProxyEntry {
/// Is the target serving HTTPS instead of HTTP? /// Is the target serving HTTPS instead of HTTP?
pub https_target: bool, pub https_target: bool,
/// Is the target the same node as we are running on? /// Flags for target selection
/// (if yes priorize it over other matching targets) pub flags: ProxyEntryFlags,
pub same_node: bool,
/// Is the target the same site as this node?
/// (if yes priorize it over other matching targets)
pub same_site: bool,
/// Add the following headers to all responses returned /// Add the following headers to all responses returned
/// when matching this rule /// when matching this rule
@ -81,6 +77,21 @@ pub struct ProxyEntry {
pub last_call: atomic::AtomicI64, pub last_call: atomic::AtomicI64,
} }
#[derive(Debug, Clone, Copy)]
pub struct ProxyEntryFlags {
/// Is the target the same node as we are running on?
/// (if yes priorize it over other matching targets)
pub same_node: bool,
/// Is the target the same site as this node?
/// (if yes priorize it over other matching targets)
pub same_site: bool,
/// Is site-wide load balancing enabled for this service?
pub site_lb: bool,
/// Is global load balancing enabled for this service?
pub global_lb: bool,
}
impl std::fmt::Display for ProxyEntry { impl std::fmt::Display for ProxyEntry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.https_target { if self.https_target {
@ -94,11 +105,16 @@ impl std::fmt::Display for ProxyEntry {
self.path_prefix.as_deref().unwrap_or_default(), self.path_prefix.as_deref().unwrap_or_default(),
self.priority self.priority
)?; )?;
if self.same_node { if self.flags.same_node {
write!(f, " OURSELF")?; write!(f, " OURSELF")?;
} else if self.same_site { } else if self.flags.same_site {
write!(f, " SAME_SITE")?; write!(f, " SAME_SITE")?;
} }
if self.flags.global_lb {
write!(f, " GLOBAL-LB")?;
} else if self.flags.site_lb {
write!(f, " SITE-LB")?;
}
if !self.add_headers.is_empty() { if !self.add_headers.is_empty() {
write!(f, " +Headers: {:?}", self.add_headers)?; write!(f, " +Headers: {:?}", self.add_headers)?;
} }
@ -126,8 +142,7 @@ fn parse_tricot_tag(
tag: &str, tag: &str,
target_addr: SocketAddr, target_addr: SocketAddr,
add_headers: &[(String, String)], add_headers: &[(String, String)],
same_node: bool, flags: ProxyEntryFlags,
same_site: bool,
) -> Option<ProxyEntry> { ) -> Option<ProxyEntry> {
let splits = tag.split(' ').collect::<Vec<_>>(); let splits = tag.split(' ').collect::<Vec<_>>();
if (splits.len() != 2 && splits.len() != 3) if (splits.len() != 2 && splits.len() != 3)
@ -162,8 +177,7 @@ fn parse_tricot_tag(
target_addr, target_addr,
https_target: (splits[0] == "tricot-https"), https_target: (splits[0] == "tricot-https"),
host, host,
same_node, flags,
same_site,
path_prefix, path_prefix,
priority, priority,
add_headers: add_headers.to_vec(), add_headers: add_headers.to_vec(),
@ -206,12 +220,19 @@ fn parse_consul_catalog(
}; };
let addr = SocketAddr::new(ip_addr, svc.port); let addr = SocketAddr::new(ip_addr, svc.port);
let (same_node, same_site) = if svc.tags.contains(&"tricot-global-lb".into()) { let (site_lb, global_lb) = if svc.tags.contains(&"tricot-global-lb".into()) {
(false, false) (false, true)
} else if svc.tags.contains(&"tricot-site-lb".into()) { } else if svc.tags.contains(&"tricot-site-lb".into()) {
(false, same_site) (true, false)
} else { } else {
(same_node, same_site) (false, false)
};
let flags = ProxyEntryFlags {
same_node,
same_site,
site_lb,
global_lb,
}; };
let mut add_headers = vec![]; let mut add_headers = vec![];
@ -227,8 +248,7 @@ fn parse_consul_catalog(
tag, tag,
addr, addr,
&add_headers[..], &add_headers[..],
same_node, flags,
same_site,
) { ) {
entries.push(ent); entries.push(ent);
} }