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
This commit is contained in:
parent
3608378097
commit
3fbfa0e0da
|
@ -196,8 +196,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_connection_internal(
|
fn create_connection_internal(
|
||||||
config: &Arc<C>,
|
config: &C,
|
||||||
connection_manager: &Arc<M>,
|
connection_manager: &M,
|
||||||
map: &mut std::sync::RwLockWriteGuard<'_, IndexMap<SocketAddr, P>>,
|
map: &mut std::sync::RwLockWriteGuard<'_, IndexMap<SocketAddr, P>>,
|
||||||
addr: &SocketAddr,
|
addr: &SocketAddr,
|
||||||
connection_pool_size: usize,
|
connection_pool_size: usize,
|
||||||
|
@ -276,31 +276,34 @@ where
|
||||||
} = match map.get(addr) {
|
} = match map.get(addr) {
|
||||||
Some(pool) => {
|
Some(pool) => {
|
||||||
let pool_status = pool.check_pool_status(self.connection_pool_size);
|
let pool_status = pool.check_pool_status(self.connection_pool_size);
|
||||||
if matches!(pool_status, PoolStatus::Empty) {
|
match pool_status {
|
||||||
// create more connection and put it in the pool
|
PoolStatus::Empty => {
|
||||||
drop(map);
|
// create more connection and put it in the pool
|
||||||
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}");
|
|
||||||
drop(map);
|
drop(map);
|
||||||
let mut map = self.map.write().unwrap();
|
self.create_connection(&mut lock_timing_ms, addr)
|
||||||
Self::create_connection_internal(
|
|
||||||
&self.connection_config,
|
|
||||||
&self.connection_manager,
|
|
||||||
&mut map,
|
|
||||||
addr,
|
|
||||||
self.connection_pool_size,
|
|
||||||
Some(&self.sender),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
CreateConnectionResult {
|
PoolStatus::PartiallyFull | PoolStatus::Full => {
|
||||||
connection,
|
let connection = pool.borrow_connection();
|
||||||
cache_hit: true,
|
if matches!(pool_status, PoolStatus::PartiallyFull) {
|
||||||
connection_cache_stats: self.stats.clone(),
|
debug!("Creating connection async for {addr}");
|
||||||
num_evictions: 0,
|
drop(map);
|
||||||
eviction_timing_ms: 0,
|
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,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue