@@ -33,13 +33,13 @@ pub struct PoolConfig {
3333impl Default for PoolConfig {
3434 fn default ( ) -> Self {
3535 Self {
36- max_size : 64 , // 增加到2的幂次,更好的内存对齐
37- min_idle : 8 , // 减少最小空闲连接
38- max_idle_time_ms : 120_000 , // 2分钟 - 进一步减少空闲时间
39- connection_timeout_ms : 3_000 , // 减少连接超时到3秒
40- retry_delay_ms : 10 , // 减少重试延迟到10ms
41- max_retries : 2 , // 减少重试次数到2次
42- max_concurrent_requests : 32 , // 增加并发请求限制到2的幂次
36+ max_size : 64 , // 增加到2的幂次,更好的内存对齐
37+ min_idle : 8 , // 减少最小空闲连接
38+ max_idle_time_ms : 120_000 , // 2分钟 - 进一步减少空闲时间
39+ connection_timeout_ms : 3_000 , // 减少连接超时到3秒
40+ retry_delay_ms : 10 , // 减少重试延迟到10ms
41+ max_retries : 2 , // 减少重试次数到2次
42+ max_concurrent_requests : 32 , // 增加并发请求限制到2的幂次
4343 max_requests_per_second : Some ( 100.0 ) , // 增加速率限制
4444 }
4545 }
@@ -259,7 +259,8 @@ impl ConnectionPoolInner {
259259 let mut connections = self . connections . lock ( ) ;
260260
261261 // 减少活跃连接计数
262- self . active_connections . fetch_sub ( 1 , std:: sync:: atomic:: Ordering :: Relaxed ) ;
262+ self . active_connections
263+ . fetch_sub ( 1 , std:: sync:: atomic:: Ordering :: Relaxed ) ;
263264
264265 // Only keep the connection if we haven't exceeded max_size
265266 if connections. len ( ) < self . config . max_size {
@@ -272,14 +273,16 @@ impl ConnectionPoolInner {
272273
273274 async fn get_connection_with_timeout ( & self ) -> Result < LocalSocketStream > {
274275 // 优化的获取连接逻辑,减少semaphore竞争
275-
276+
276277 // 首先快速检查是否有可用的池化连接
277278 if let Some ( stream) = self . get_pooled_connection ( ) {
278279 return Ok ( stream) ;
279280 }
280281
281282 // 检查活跃连接数,避免不必要的semaphore等待
282- let active_count = self . active_connections . load ( std:: sync:: atomic:: Ordering :: Relaxed ) ;
283+ let active_count = self
284+ . active_connections
285+ . load ( std:: sync:: atomic:: Ordering :: Relaxed ) ;
283286 if active_count >= self . config . max_size {
284287 // 快速失败路径,避免长时间等待
285288 return Err ( KodeBridgeError :: custom ( "Connection pool exhausted" ) ) ;
@@ -289,9 +292,7 @@ impl ConnectionPoolInner {
289292 let timeout = std:: cmp:: min ( self . config . connection_timeout ( ) , Duration :: from_millis ( 500 ) ) ;
290293 let permit = tokio:: time:: timeout ( timeout, self . semaphore . acquire ( ) )
291294 . await
292- . map_err ( |_| {
293- KodeBridgeError :: timeout ( timeout. as_millis ( ) as u64 )
294- } ) ?
295+ . map_err ( |_| KodeBridgeError :: timeout ( timeout. as_millis ( ) as u64 ) ) ?
295296 . map_err ( |_| KodeBridgeError :: custom ( "Semaphore closed" ) ) ?;
296297
297298 // 再次检查池化连接(避免不必要的连接创建)
@@ -301,7 +302,8 @@ impl ConnectionPoolInner {
301302 }
302303
303304 // 增加活跃连接计数
304- self . active_connections . fetch_add ( 1 , std:: sync:: atomic:: Ordering :: Relaxed ) ;
305+ self . active_connections
306+ . fetch_add ( 1 , std:: sync:: atomic:: Ordering :: Relaxed ) ;
305307
306308 // 创建新连接
307309 match self . create_connection ( ) . await {
@@ -311,7 +313,8 @@ impl ConnectionPoolInner {
311313 }
312314 Err ( e) => {
313315 // 出错时减少活跃连接计数
314- self . active_connections . fetch_sub ( 1 , std:: sync:: atomic:: Ordering :: Relaxed ) ;
316+ self . active_connections
317+ . fetch_sub ( 1 , std:: sync:: atomic:: Ordering :: Relaxed ) ;
315318 Err ( e)
316319 }
317320 }
@@ -394,7 +397,10 @@ impl ConnectionPool {
394397 /// Get pool statistics
395398 pub fn stats ( & self ) -> PoolStats {
396399 let connections = self . inner . connections . lock ( ) ;
397- let active_count = self . inner . active_connections . load ( std:: sync:: atomic:: Ordering :: Relaxed ) ;
400+ let active_count = self
401+ . inner
402+ . active_connections
403+ . load ( std:: sync:: atomic:: Ordering :: Relaxed ) ;
398404 PoolStats {
399405 total_connections : connections. len ( ) ,
400406 available_permits : self . inner . semaphore . available_permits ( ) ,
0 commit comments