diff --git a/src/utils/compare.rs b/src/utils/compare.rs index ae33805..86f6644 100644 --- a/src/utils/compare.rs +++ b/src/utils/compare.rs @@ -1,9 +1,8 @@ use crate::utils::tools::*; use std::collections::HashSet; -use tokio::sync::RwLockReadGuard; // #[allow(dead_code)] -pub fn dm(map1: &RwLockReadGuard, map2: &UpstreamMap) -> bool { +pub fn dm(map1: &UpstreamMap, map2: &UpstreamMap) -> bool { if map1.len() != map2.len() { return false; // Different number of keys } diff --git a/src/utils/healthcheck.rs b/src/utils/healthcheck.rs index 34672e3..7ba62b6 100644 --- a/src/utils/healthcheck.rs +++ b/src/utils/healthcheck.rs @@ -3,10 +3,9 @@ use dashmap::DashMap; use std::sync::atomic::AtomicUsize; use std::sync::Arc; use std::time::Duration; -use tokio::sync::RwLock; use tokio::time::interval; -pub async fn hc(upslist: Arc>, fullist: Arc>) { +pub async fn hc(upslist: Arc, fullist: Arc) { let mut period = interval(Duration::from_secs(2)); loop { @@ -18,8 +17,7 @@ pub async fn hc(upslist: Arc>, fullist: Arc>, fullist: Arc {:?}", vv); } - upsl.insert(k, v); + upslist.insert(k, v); } } } diff --git a/src/web/proxyhttp.rs b/src/web/proxyhttp.rs index 48dd69c..a03ed2f 100644 --- a/src/web/proxyhttp.rs +++ b/src/web/proxyhttp.rs @@ -13,12 +13,11 @@ use pingora_http::{RequestHeader, ResponseHeader}; use pingora_proxy::{ProxyHttp, Session}; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; -use tokio::sync::RwLock; // use tokio::time::Instant; pub struct LB { - pub upstreams: Arc>, - pub umap_full: Arc>, + pub upstreams: Arc, + pub umap_full: Arc, } #[async_trait] @@ -49,31 +48,21 @@ impl BackgroundService for LB { val = rx.next() => { match val { Some(newmap) => { - let umap_work = self.upstreams.read().await; - let umap_full = self.umap_full.read().await; - match compare::dm(&umap_full, &newmap) { + match compare::dm(&self.umap_full, &newmap) { false => { - drop(umap_full); - drop(umap_work); - let work = self.upstreams.write().await; - let full = self.umap_full.write().await; - work.clear(); - full.clear(); + self.upstreams.clear(); + self.umap_full.clear(); for (k,v) in newmap { println!("Host: {}", k); // {:?}", vv); } - work.insert(k.clone(), (v.0.clone(), AtomicUsize::new(0))); // No need for extra vec! - full.insert(k, (v.0, AtomicUsize::new(0))); // Use `value.0` directly + self.upstreams.insert(k.clone(), (v.0.clone(), AtomicUsize::new(0))); // No need for extra vec! + self.umap_full.insert(k, (v.0, AtomicUsize::new(0))); // Use `value.0` directly } - drop(full); - drop(work); } true => { - drop(umap_full); - drop(umap_work); } } } @@ -92,8 +81,7 @@ pub trait GetHost { #[async_trait] impl GetHost for LB { async fn get_host(&self, peer: &str) -> Option<(String, u16)> { - let map_read = self.upstreams.read().await; - let x = if let Some(entry) = map_read.get(peer) { + let x = if let Some(entry) = self.upstreams.get(peer) { let (servers, index) = entry.value(); if servers.is_empty() { return None; @@ -104,7 +92,6 @@ impl GetHost for LB { } else { None }; - drop(map_read); x } } diff --git a/src/web/start.rs b/src/web/start.rs index a456ca8..aa45df0 100644 --- a/src/web/start.rs +++ b/src/web/start.rs @@ -4,7 +4,6 @@ use dashmap::DashMap; use pingora_core::prelude::background_service; use pingora_core::server::Server; use std::sync::Arc; -use tokio::sync::RwLock; pub fn run() { env_logger::init(); @@ -13,10 +12,10 @@ pub fn run() { server.bootstrap(); let upstreams_map: UpstreamMap = DashMap::new(); - let config = Arc::new(RwLock::new(upstreams_map)); + let config = Arc::new(upstreams_map); let umap_full: UpstreamMap = DashMap::new(); - let fconfig = Arc::new(RwLock::new(umap_full)); + let fconfig = Arc::new(umap_full); let lb = LB { upstreams: config.clone(),