Skip to content

Commit 0ad40be

Browse files
committed
refactor: Clean up code formatting and improve readability in PoolConfig and StreamingResponse
1 parent fd0ba22 commit 0ad40be

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ mod tests {
194194
assert_eq!(pool_config.max_idle_time_ms, 300_000);
195195

196196
let default_config = PoolConfig::default();
197-
assert_eq!(default_config.max_size, 64); // 更新为新的默认值
198-
assert_eq!(default_config.min_idle, 8); // 更新为新的默认值
197+
assert_eq!(default_config.max_size, 64); // 更新为新的默认值
198+
assert_eq!(default_config.min_idle, 8); // 更新为新的默认值
199199
}
200200

201201
#[cfg(feature = "server")]

src/pool.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ pub struct PoolConfig {
3333
impl 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(),

src/stream_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl StreamingResponse {
246246
/// Collect stream data with a timeout - optimized for better performance
247247
pub async fn collect_text_with_timeout(mut self, timeout: Duration) -> Result<String> {
248248
let mut body_lines = Vec::new();
249-
249+
250250
// 限制最大超时时间避免长时间waker等待
251251
let optimized_timeout = std::cmp::min(timeout, Duration::from_secs(30));
252252
let timeout_future = tokio::time::sleep(optimized_timeout);

0 commit comments

Comments
 (0)