performance improvement, sticky session minor bug fix

This commit is contained in:
Ara Sadoyan
2026-03-17 19:21:05 +01:00
parent c9422759aa
commit 24d00da855
2 changed files with 12 additions and 10 deletions

View File

@@ -166,7 +166,7 @@ pub fn clone_idmap_into(original: &UpstreamsDashMap, cloned: &UpstreamsIdMap) {
let new_vec = vec.clone(); let new_vec = vec.clone();
for x in vec.iter() { for x in vec.iter() {
let mut id = String::new(); let mut id = String::new();
write!(&mut id, "{}:{}:{}", x.address, x.port, x.is_ssl).unwrap(); write!(&mut id, "{}:{}:{}:{}", outer_entry.key(), x.address, x.port, x.is_ssl).unwrap();
let mut hasher = Sha256::new(); let mut hasher = Sha256::new();
hasher.update(id.clone().into_bytes()); hasher.update(id.clone().into_bytes());
let hash = hasher.finalize(); let hash = hasher.finalize();

View File

@@ -50,7 +50,7 @@ pub struct Context {
hostname: Option<Arc<str>>, hostname: Option<Arc<str>>,
upstream_peer: Option<Arc<InnerMap>>, upstream_peer: Option<Arc<InnerMap>>,
extraparams: arc_swap::Guard<Arc<Extraparams>>, extraparams: arc_swap::Guard<Arc<Extraparams>>,
client_headers: Option<Arc<Vec<(Arc<str>, Arc<str>)>>>, client_headers: Option<Vec<(Arc<str>, Arc<str>)>>,
} }
#[async_trait] #[async_trait]
@@ -181,7 +181,8 @@ impl ProxyHttp for LB {
if ctx.extraparams.sticky_sessions { if ctx.extraparams.sticky_sessions {
let mut s = String::with_capacity(64); let mut s = String::with_capacity(64);
write!(&mut s, "{}:{}:{}", innermap.address, innermap.port, innermap.is_ssl).unwrap(); write!(&mut s, "{}:{}:{}:{}", hostname, innermap.address, innermap.port, innermap.is_ssl).unwrap();
// write!(&mut s, "{}:{}:{}", innermap.address, innermap.port, innermap.is_ssl).unwrap();
ctx.backend_id = Some(s); ctx.backend_id = Some(s);
ctx.sticky_sessions = true; ctx.sticky_sessions = true;
} }
@@ -241,25 +242,26 @@ impl ProxyHttp for LB {
} }
} }
if let Some(ch) = client_headers { if let Some(ch) = client_headers {
ctx.client_headers = Some(Arc::new(ch)); ctx.client_headers = Some(ch);
} }
Ok(()) Ok(())
} }
async fn response_filter(&self, session: &mut Session, _upstream_response: &mut ResponseHeader, ctx: &mut Self::CTX) -> Result<()> { async fn response_filter(&self, session: &mut Session, _upstream_response: &mut ResponseHeader, ctx: &mut Self::CTX) -> Result<()> {
if ctx.sticky_sessions { if ctx.sticky_sessions {
if let Some(bid) = &ctx.backend_id { if let Some(bid) = &ctx.backend_id {
if REVERSE_STORE.get(bid).is_none() { let tt = if let Some(existing) = REVERSE_STORE.get(bid) {
existing.value().clone()
} else {
let mut hasher = Sha256::new(); let mut hasher = Sha256::new();
hasher.update(bid.as_bytes()); hasher.update(bid.as_bytes());
let hash = hasher.finalize(); let hash = hasher.finalize();
let hex_hash = base16ct::lower::encode_string(&hash); let hex_hash = base16ct::lower::encode_string(&hash);
let hh = hex_hash[0..50].to_string(); let hh = hex_hash[0..50].to_string();
REVERSE_STORE.insert(bid.clone(), hh.clone()); REVERSE_STORE.insert(bid.clone(), hh.clone());
REVERSE_STORE.insert(hh, bid.clone()); REVERSE_STORE.insert(hh.clone(), bid.clone());
} hh
if let Some(tt) = REVERSE_STORE.get(bid) { };
let _ = _upstream_response.insert_header("set-cookie", format!("backend_id={}; Path=/; Max-Age=600; HttpOnly; SameSite=Lax", tt.value())); let _ = _upstream_response.insert_header("set-cookie", format!("backend_id={}; Path=/; Max-Age=600; HttpOnly; SameSite=Lax", tt));
}
} }
} }