diff --git a/src/utils/tools.rs b/src/utils/tools.rs index 5faec8f..45bb88b 100644 --- a/src/utils/tools.rs +++ b/src/utils/tools.rs @@ -166,7 +166,7 @@ pub fn clone_idmap_into(original: &UpstreamsDashMap, cloned: &UpstreamsIdMap) { let new_vec = vec.clone(); for x in vec.iter() { 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(); hasher.update(id.clone().into_bytes()); let hash = hasher.finalize(); diff --git a/src/web/proxyhttp.rs b/src/web/proxyhttp.rs index a930b1e..0727b12 100644 --- a/src/web/proxyhttp.rs +++ b/src/web/proxyhttp.rs @@ -50,7 +50,7 @@ pub struct Context { hostname: Option>, upstream_peer: Option>, extraparams: arc_swap::Guard>, - client_headers: Option, Arc)>>>, + client_headers: Option, Arc)>>, } #[async_trait] @@ -181,7 +181,8 @@ impl ProxyHttp for LB { if ctx.extraparams.sticky_sessions { 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.sticky_sessions = true; } @@ -241,25 +242,26 @@ impl ProxyHttp for LB { } } if let Some(ch) = client_headers { - ctx.client_headers = Some(Arc::new(ch)); + ctx.client_headers = Some(ch); } Ok(()) } async fn response_filter(&self, session: &mut Session, _upstream_response: &mut ResponseHeader, ctx: &mut Self::CTX) -> Result<()> { if ctx.sticky_sessions { 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(); hasher.update(bid.as_bytes()); let hash = hasher.finalize(); let hex_hash = base16ct::lower::encode_string(&hash); let hh = hex_hash[0..50].to_string(); REVERSE_STORE.insert(bid.clone(), hh.clone()); - REVERSE_STORE.insert(hh, bid.clone()); - } - 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())); - } + REVERSE_STORE.insert(hh.clone(), bid.clone()); + hh + }; + let _ = _upstream_response.insert_header("set-cookie", format!("backend_id={}; Path=/; Max-Age=600; HttpOnly; SameSite=Lax", tt)); } }