From 6ef7f2382317da45bf12091658720f4e9ecda4d7 Mon Sep 17 00:00:00 2001 From: Ara Sadoyan Date: Wed, 28 Jan 2026 13:20:31 +0100 Subject: [PATCH] Performance optimization v2 --- src/utils/httpclient.rs | 6 +++--- src/utils/parceyaml.rs | 2 +- src/utils/structs.rs | 40 +++++++++++++++++++++++++--------------- src/utils/tools.rs | 21 ++++++++++++++++----- src/web/proxyhttp.rs | 4 ++-- 5 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/utils/httpclient.rs b/src/utils/httpclient.rs index ed1c850..f681c76 100644 --- a/src/utils/httpclient.rs +++ b/src/utils/httpclient.rs @@ -25,10 +25,10 @@ pub async fn for_consul(url: String, token: Option, conf: &ServiceMappin for subsets in endpoints { // 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 addr = subsets.tagged_addresses.get("lan_ipv4").unwrap().address.clone(); let prt = subsets.tagged_addresses.get("lan_ipv4").unwrap().port.clone(); let to_add = Arc::from(InnerMap { - address: addr, + address: Arc::from(&*addr), port: prt, is_ssl: false, is_http2: false, @@ -61,7 +61,7 @@ pub async fn for_kuber(url: &str, token: &str, conf: &ServiceMapping) -> Option< for addr in addresses { for port in &ports { let to_add = Arc::from(InnerMap { - address: addr.ip.parse().unwrap(), + address: Arc::from(addr.ip.clone()), port: port.port.clone(), is_ssl: false, is_http2: false, diff --git a/src/utils/parceyaml.rs b/src/utils/parceyaml.rs index 8f7f24c..c02e9d8 100644 --- a/src/utils/parceyaml.rs +++ b/src/utils/parceyaml.rs @@ -136,7 +136,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(Arc::from(InnerMap { - address: ip.trim().parse().unwrap(), + address: Arc::from(ip), port, is_ssl: true, is_http2: false, diff --git a/src/utils/structs.rs b/src/utils/structs.rs index 706cf9c..f2b3807 100644 --- a/src/utils/structs.rs +++ b/src/utils/structs.rs @@ -1,7 +1,6 @@ use dashmap::DashMap; use serde::{Deserialize, Serialize}; use std::collections::HashMap; -use std::net::IpAddr; use std::sync::atomic::AtomicUsize; use std::sync::Arc; @@ -10,6 +9,14 @@ pub type UpstreamsDashMap = DashMap, DashMap, (Vec, Arc>; pub type Headers = DashMap, DashMap, Vec<(Arc, Arc)>>>; +#[derive(Clone, Debug, Default)] +pub struct Extraparams { + pub sticky_sessions: bool, + pub to_https: Option, + pub authentication: DashMap, Vec>>, + pub rate_limit: Option, +} + #[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct ServiceMapping { pub upstream: String, @@ -21,13 +28,6 @@ pub struct ServiceMapping { pub server_headers: Option>, } -#[derive(Clone, Debug, Default)] -pub struct Extraparams { - pub sticky_sessions: bool, - pub to_https: Option, - pub authentication: DashMap, Vec>>, - pub rate_limit: Option, -} #[derive(Clone, Default, Debug, Serialize, Deserialize)] pub struct Kubernetes { pub servers: Option>, @@ -114,9 +114,9 @@ pub struct AppConfig { pub rungroup: Option, } -#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct InnerMap { - pub address: IpAddr, + pub address: Arc, pub port: u16, pub is_ssl: bool, pub is_http2: bool, @@ -129,7 +129,8 @@ pub struct InnerMap { impl InnerMap { pub fn new() -> Self { Self { - address: "127.0.0.1".parse().unwrap(), + // address: "127.0.0.1".parse().unwrap(), + address: Arc::from("127.0.0.1"), port: Default::default(), is_ssl: Default::default(), is_http2: Default::default(), @@ -140,9 +141,18 @@ impl InnerMap { } } -#[derive(Serialize)] -pub struct UpstreamSnapshot { - pub backends: Vec, +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct InnerMapForJson { + pub address: String, + pub port: u16, + pub is_ssl: bool, + pub is_http2: bool, + pub to_https: bool, + pub rate_limit: Option, + pub healthcheck: Option, +} +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct UpstreamSnapshotForJson { + pub backends: Vec, pub requests: usize, } -// pub type UpstreamsSnapshot = HashMap>; diff --git a/src/utils/tools.rs b/src/utils/tools.rs index fb20468..4ef1721 100644 --- a/src/utils/tools.rs +++ b/src/utils/tools.rs @@ -1,4 +1,4 @@ -use crate::utils::structs::{InnerMap, UpstreamSnapshot, UpstreamsDashMap, UpstreamsIdMap}; +use crate::utils::structs::{InnerMap, InnerMapForJson, UpstreamSnapshotForJson, UpstreamsDashMap, UpstreamsIdMap}; use crate::utils::tls; use crate::utils::tls::CertificateConfig; use dashmap::DashMap; @@ -161,7 +161,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: "127.0.0.1".parse().unwrap(), + address: Arc::from("127.0.0.1"), port: 0, is_ssl: false, is_http2: false, @@ -283,8 +283,19 @@ pub fn upstreams_to_json(upstreams: &UpstreamsDashMap) -> serde_json::Result match ctx.upstream_peer.as_ref() { Some(innermap) => { - let mut peer = Box::new(HttpPeer::new((innermap.address, innermap.port), innermap.is_ssl, String::new())); + let mut peer = Box::new(HttpPeer::new((&*innermap.address, innermap.port), innermap.is_ssl, String::new())); if innermap.is_http2 { peer.options.alpn = ALPN::H2; } @@ -182,7 +182,7 @@ impl ProxyHttp for LB { upstream_request.insert_header("Host", hostname.as_ref())?; } if let Some(peer) = ctx.upstream_peer.as_ref() { - upstream_request.insert_header("X-Forwarded-For", peer.address.to_string())?; + upstream_request.insert_header("X-Forwarded-For", &*peer.address)?; } if let Some(headers) = self.get_header(ctx.hostname.as_ref().unwrap_or(&Arc::from("localhost")), session.req_header().uri.path()) {