fix: 修复数据库连接池的关键bug和配置优化
- 修复连接池try_acquire_from_pool方法的严重bug:之前即使找到可用连接也返回None - 正确实现连接的获取、移除和返回逻辑 - 优化连接池配置:减少最大连接数从10到5,最小连接数从3到2 - 减少获取超时从30秒到10秒,实现快速失败 - 改进日志记录:添加连接获取和归还的详细状态日志 - 修复AppState测试构造函数中缺失的outfit_photo_generation_repository字段 这个修复解决了应用启动时连接池连接超时导致卡死的问题。
This commit is contained in:
parent
852d4c7fa2
commit
08cd544cfe
|
|
@ -197,6 +197,7 @@ impl AppState {
|
|||
model_dynamic_repository: Mutex::new(None),
|
||||
video_generation_repository: Mutex::new(None),
|
||||
conversation_repository: Mutex::new(None),
|
||||
outfit_photo_generation_repository: Mutex::new(None),
|
||||
performance_monitor: Mutex::new(PerformanceMonitor::new()),
|
||||
event_bus_manager: Arc::new(EventBusManager::new()),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ pub mod draft_parser_tests;
|
|||
pub mod cloud_upload_service_tests;
|
||||
pub mod watermark_tests;
|
||||
pub mod template_service_tests;
|
||||
pub mod template_foreign_key_test;
|
||||
|
||||
// 测试工具函数
|
||||
pub mod test_utils {
|
||||
|
|
|
|||
|
|
@ -231,18 +231,23 @@ impl ConnectionPool {
|
|||
fn try_acquire_from_pool(&self) -> Result<Option<Connection>> {
|
||||
let mut connections = self.connections.lock().map_err(|e| anyhow!("获取连接池锁失败: {}", e))?;
|
||||
|
||||
// 查找可用的连接
|
||||
for pooled_conn in connections.iter_mut() {
|
||||
// 清理过期连接
|
||||
connections.retain(|conn| !conn.is_expired(self.config.idle_timeout));
|
||||
|
||||
// 查找可用的连接并移除它
|
||||
let mut found_index = None;
|
||||
for (i, pooled_conn) in connections.iter().enumerate() {
|
||||
if !pooled_conn.is_busy && !pooled_conn.is_expired(self.config.idle_timeout) {
|
||||
pooled_conn.mark_used();
|
||||
// 这里我们需要移除连接,但由于借用检查器的限制,我们需要重新设计
|
||||
// 暂时返回 None,后续优化
|
||||
found_index = Some(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 清理过期连接
|
||||
connections.retain(|conn| !conn.is_expired(self.config.idle_timeout));
|
||||
if let Some(index) = found_index {
|
||||
let pooled_conn = connections.remove(index).unwrap();
|
||||
println!("从连接池获取现有连接,剩余连接数: {}", connections.len());
|
||||
return Ok(Some(pooled_conn.connection));
|
||||
}
|
||||
|
||||
// 如果没有可用连接且未达到最大连接数,创建新连接
|
||||
if connections.len() < self.config.max_connections {
|
||||
|
|
@ -259,6 +264,7 @@ impl ConnectionPool {
|
|||
}
|
||||
}
|
||||
|
||||
println!("连接池已满且无可用连接,当前连接数: {}", connections.len());
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
|
|
@ -271,6 +277,8 @@ impl ConnectionPool {
|
|||
stats.total_returned += 1;
|
||||
stats.active_connections = stats.active_connections.saturating_sub(1);
|
||||
stats.idle_connections += 1;
|
||||
|
||||
println!("连接已归还到池中,当前连接数: {}", connections.len());
|
||||
}
|
||||
|
||||
/// 获取连接池统计信息
|
||||
|
|
|
|||
|
|
@ -213,10 +213,10 @@ impl Database {
|
|||
} else {
|
||||
println!("🚨 强制启用默认连接池配置以避免死锁");
|
||||
let default_config = ConnectionPoolConfig {
|
||||
max_connections: 10, // 适中的最大连接数
|
||||
min_connections: 3, // 保持最小连接数
|
||||
max_connections: 5, // 减少最大连接数,SQLite不适合太多并发
|
||||
min_connections: 2, // 减少最小连接数
|
||||
connection_timeout: Duration::from_secs(30), // 连接超时
|
||||
acquire_timeout: Duration::from_secs(30), // 增加获取超时
|
||||
acquire_timeout: Duration::from_secs(10), // 减少获取超时,快速失败
|
||||
idle_timeout: Duration::from_secs(300), // 5分钟空闲超时
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue