From 8ba8d32df1f06a57197d0b2380fa3462109ac082 Mon Sep 17 00:00:00 2001 From: Ara Sadoyan Date: Wed, 26 Nov 2025 12:12:41 +0100 Subject: [PATCH] Performance improvements, type changes --- src/utils/healthcheck.rs | 2 +- src/utils/httpclient.rs | 6 ++++-- src/utils/kuberconsul.rs | 3 +-- src/utils/parceyaml.rs | 2 +- src/utils/structs.rs | 6 +++--- src/utils/tools.rs | 2 +- src/web/gethosts.rs | 31 +++++++++++++++---------------- src/web/proxyhttp.rs | 2 +- 8 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/utils/healthcheck.rs b/src/utils/healthcheck.rs index bceba9d..91f6e19 100644 --- a/src/utils/healthcheck.rs +++ b/src/utils/healthcheck.rs @@ -46,7 +46,7 @@ async fn build_upstreams(fullist: &UpstreamsDashMap, method: &str, client: &Clie let mut innervec = Vec::new(); for (_, upstream) in path_entry.value().0.iter().enumerate() { - let tls = detect_tls(upstream.address.as_str(), &upstream.port, &client).await; + let tls = detect_tls(&upstream.address.to_string(), &upstream.port, &client).await; let is_h2 = matches!(tls.1, Some(Version::HTTP_2)); let link = if tls.0 { diff --git a/src/utils/httpclient.rs b/src/utils/httpclient.rs index 2997ceb..2a107e4 100644 --- a/src/utils/httpclient.rs +++ b/src/utils/httpclient.rs @@ -22,7 +22,9 @@ pub async fn for_consul(url: String, token: Option, conf: &ServiceMappin let upstreams: DashMap, AtomicUsize)> = DashMap::new(); let endpoints: Vec = resp.json().await.ok()?; for subsets in endpoints { - let addr = subsets.tagged_addresses.get("lan_ipv4").unwrap().address.clone(); + // let addr = subsets.tagged_addresses.get("lan_ipv4").unwrap().address.clone(); + // let prt = subsets.tagged_addresses.get("lan_ipv4").unwrap().port.clone(); + let addr = subsets.tagged_addresses.get("lan_ipv4").unwrap().address.clone().parse().unwrap(); let prt = subsets.tagged_addresses.get("lan_ipv4").unwrap().port.clone(); let to_add = InnerMap { address: addr, @@ -56,7 +58,7 @@ pub async fn for_kuber(url: &str, token: &str, conf: &ServiceMapping) -> Option< for addr in addresses { for port in &ports { let to_add = InnerMap { - address: addr.ip.clone(), + address: addr.ip.parse().unwrap(), port: port.port.clone(), is_ssl: false, is_http2: false, diff --git a/src/utils/kuberconsul.rs b/src/utils/kuberconsul.rs index da62238..5a0a0e4 100644 --- a/src/utils/kuberconsul.rs +++ b/src/utils/kuberconsul.rs @@ -108,10 +108,9 @@ impl ServiceDiscovery for KubernetesDiscovery { let num = if end > 0 { rand::rng().random_range(0..end) } else { 0 }; let server = servers.get(num).unwrap().to_string(); let path = kuber.tokenpath.unwrap_or("/var/run/secrets/kubernetes.io/serviceaccount/token".to_string()); - let namespace = get_current_namespace().unwrap_or_else(|| "staging".to_string()); + let namespace = get_current_namespace().unwrap_or_else(|| "default".to_string()); let token = read_token(path.as_str()).await; loop { - // crate::utils::watchksecret::watch_secret("ar-tls", "staging", server.clone(), token.clone(), &mut oldcrt).await; let upstreams = UpstreamsDashMap::new(); if let Some(kuber) = config.kubernetes.clone() { if let Some(svc) = kuber.services { diff --git a/src/utils/parceyaml.rs b/src/utils/parceyaml.rs index 77c04f9..fdac89e 100644 --- a/src/utils/parceyaml.rs +++ b/src/utils/parceyaml.rs @@ -135,7 +135,7 @@ async fn populate_file_upstreams(config: &mut Configuration, parsed: &Config) { if let Some((ip, port_str)) = server.split_once(':') { if let Ok(port) = port_str.parse::() { server_list.push(InnerMap { - address: ip.trim().to_string(), + address: ip.trim().parse().unwrap(), port, is_ssl: true, is_http2: false, diff --git a/src/utils/structs.rs b/src/utils/structs.rs index ab9d598..d15b931 100644 --- a/src/utils/structs.rs +++ b/src/utils/structs.rs @@ -2,8 +2,8 @@ use dashmap::DashMap; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::atomic::AtomicUsize; - pub type UpstreamsDashMap = DashMap, AtomicUsize)>>; +use std::net::IpAddr; pub type UpstreamsIdMap = DashMap; pub type Headers = DashMap>>; @@ -116,7 +116,7 @@ pub struct AppConfig { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct InnerMap { - pub address: String, + pub address: IpAddr, pub port: u16, pub is_ssl: bool, pub is_http2: bool, @@ -129,7 +129,7 @@ pub struct InnerMap { impl InnerMap { pub fn new() -> Self { Self { - address: Default::default(), + address: "127.0.0.1".parse().unwrap(), port: Default::default(), is_ssl: Default::default(), is_http2: Default::default(), diff --git a/src/utils/tools.rs b/src/utils/tools.rs index b5efbd0..3977eee 100644 --- a/src/utils/tools.rs +++ b/src/utils/tools.rs @@ -159,7 +159,7 @@ pub fn clone_idmap_into(original: &UpstreamsDashMap, cloned: &UpstreamsIdMap) { let hex_hash = base16ct::lower::encode_string(&hash); let hh = hex_hash[0..50].to_string(); let to_add = InnerMap { - address: hh.clone(), + address: "127.0.0.1".parse().unwrap(), port: 0, is_ssl: false, is_http2: false, diff --git a/src/web/gethosts.rs b/src/web/gethosts.rs index 45ef7fe..fe716f9 100644 --- a/src/web/gethosts.rs +++ b/src/web/gethosts.rs @@ -11,7 +11,10 @@ pub struct GetHostsReturHeaders { #[async_trait] pub trait GetHost { + // fn get_host<'a>(&self, peer: &str, path: &str, backend_id: Option<&str>) -> Option<&'a InnerMap>; + fn get_host(&self, peer: &str, path: &str, backend_id: Option<&str>) -> Option; + fn get_header(&self, peer: &str, path: &str) -> Option; } #[async_trait] @@ -22,35 +25,31 @@ impl GetHost for LB { return Some(bb.value().clone()); } } - let host_entry = self.ump_upst.get(peer)?; - let mut current_path = path.to_string(); - let mut best_match: Option = None; + let mut end = path.len(); loop { - if let Some(entry) = host_entry.get(¤t_path) { + let slice = &path[..end]; + if let Some(entry) = host_entry.get(slice) { let (servers, index) = entry.value(); if !servers.is_empty() { let idx = index.fetch_add(1, Ordering::Relaxed) % servers.len(); - best_match = Some(servers[idx].clone()); - break; + return Some(servers[idx].clone()); } } - if let Some(pos) = current_path.rfind('/') { - current_path.truncate(pos); + if let Some(pos) = slice.rfind('/') { + end = pos; } else { break; } } - if best_match.is_none() { - if let Some(entry) = host_entry.get("/") { - let (servers, index) = entry.value(); - if !servers.is_empty() { - let idx = index.fetch_add(1, Ordering::Relaxed) % servers.len(); - best_match = Some(servers[idx].clone()); - } + if let Some(entry) = host_entry.get("/") { + let (servers, index) = entry.value(); + if !servers.is_empty() { + let idx = index.fetch_add(1, Ordering::Relaxed) % servers.len(); + return Some(servers[idx].clone()); } } - best_match + None } fn get_header(&self, peer: &str, path: &str) -> Option { diff --git a/src/web/proxyhttp.rs b/src/web/proxyhttp.rs index f33caee..f3f200a 100644 --- a/src/web/proxyhttp.rs +++ b/src/web/proxyhttp.rs @@ -186,7 +186,7 @@ impl ProxyHttp for LB { upstream_request.insert_header("Host", hostname)?; } if let Some(peer) = ctx.upstream_peer.as_ref() { - upstream_request.insert_header("X-Forwarded-For", peer.address.as_str())?; + upstream_request.insert_header("X-Forwarded-For", peer.address.to_string())?; } if let Some(headers) = self.get_header(ctx.hostname.as_ref().unwrap_or(&"localhost".to_string()), session.req_header().uri.path()) {