From 3fbfa0e0da278041331710f60d4806218a806af3 Mon Sep 17 00:00:00 2001 From: Lijun Wang <83639177+lijunwangs@users.noreply.github.com> Date: Wed, 27 Sep 2023 09:35:19 -0700 Subject: [PATCH] Simplify code and use match to harden logic (#33409) addressed more feedback from Jon: Simplify code and use match to harden logic in connection cache --- connection-cache/src/connection_cache.rs | 53 +++++++++++++----------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/connection-cache/src/connection_cache.rs b/connection-cache/src/connection_cache.rs index 306a8df272..4962f815c3 100644 --- a/connection-cache/src/connection_cache.rs +++ b/connection-cache/src/connection_cache.rs @@ -196,8 +196,8 @@ where } fn create_connection_internal( - config: &Arc, - connection_manager: &Arc, + config: &C, + connection_manager: &M, map: &mut std::sync::RwLockWriteGuard<'_, IndexMap>, addr: &SocketAddr, connection_pool_size: usize, @@ -276,31 +276,34 @@ where } = match map.get(addr) { Some(pool) => { let pool_status = pool.check_pool_status(self.connection_pool_size); - if matches!(pool_status, PoolStatus::Empty) { - // create more connection and put it in the pool - drop(map); - self.create_connection(&mut lock_timing_ms, addr) - } else { - let connection = pool.borrow_connection(); - if matches!(pool_status, PoolStatus::PartiallyFull) { - debug!("Creating connection async for {addr}"); + match pool_status { + PoolStatus::Empty => { + // create more connection and put it in the pool drop(map); - let mut map = self.map.write().unwrap(); - Self::create_connection_internal( - &self.connection_config, - &self.connection_manager, - &mut map, - addr, - self.connection_pool_size, - Some(&self.sender), - ); + self.create_connection(&mut lock_timing_ms, addr) } - CreateConnectionResult { - connection, - cache_hit: true, - connection_cache_stats: self.stats.clone(), - num_evictions: 0, - eviction_timing_ms: 0, + PoolStatus::PartiallyFull | PoolStatus::Full => { + let connection = pool.borrow_connection(); + if matches!(pool_status, PoolStatus::PartiallyFull) { + debug!("Creating connection async for {addr}"); + drop(map); + let mut map = self.map.write().unwrap(); + Self::create_connection_internal( + &self.connection_config, + &self.connection_manager, + &mut map, + addr, + self.connection_pool_size, + Some(&self.sender), + ); + } + CreateConnectionResult { + connection, + cache_hit: true, + connection_cache_stats: self.stats.clone(), + num_evictions: 0, + eviction_timing_ms: 0, + } } } }