|
|
@@ -62,7 +62,7 @@ class StreamingSession:
|
|
|
if not self.doubao_client:
|
|
|
await self.send_error(
|
|
|
ErrorCode.CONNECTION_POOL_EXHAUSTED,
|
|
|
- "连接池已耗尽"
|
|
|
+ "连接池已耗尽,请稍后重试"
|
|
|
)
|
|
|
return False
|
|
|
|
|
|
@@ -71,6 +71,10 @@ class StreamingSession:
|
|
|
|
|
|
except Exception as e:
|
|
|
logger.error(f"启动会话失败: {e}")
|
|
|
+ # 如果获取连接后出错,需要立即释放
|
|
|
+ if self.doubao_client:
|
|
|
+ await connection_pool.release(self.doubao_client)
|
|
|
+ self.doubao_client = None
|
|
|
await self.send_error(ErrorCode.INTERNAL_ERROR, f"启动会话失败: {str(e)}")
|
|
|
return False
|
|
|
|
|
|
@@ -576,6 +580,8 @@ class StreamingSession:
|
|
|
async def cleanup(self):
|
|
|
"""清理会话"""
|
|
|
try:
|
|
|
+ logger.info(f"开始清理会话 {self.session_id}...")
|
|
|
+
|
|
|
# 记录会话
|
|
|
performance_monitor.record_session()
|
|
|
|
|
|
@@ -586,15 +592,26 @@ class StreamingSession:
|
|
|
|
|
|
# 注意:ai_response_end已在receive_from_doubao中发送,这里不重复发送
|
|
|
|
|
|
- # 归还连接到池中
|
|
|
+ # 归还连接到池中(关键!必须释放)
|
|
|
if self.doubao_client:
|
|
|
+ logger.debug(f"释放会话 {self.session_id} 的豆包连接到连接池")
|
|
|
await connection_pool.release(self.doubao_client)
|
|
|
self.doubao_client = None
|
|
|
+ else:
|
|
|
+ logger.warning(f"会话 {self.session_id} 没有豆包连接需要释放")
|
|
|
|
|
|
logger.info(f"会话 {self.session_id} 已清理 - 收到: {self.audio_received_bytes}B, 发送: {self.audio_sent_bytes}B")
|
|
|
|
|
|
except Exception as e:
|
|
|
logger.error(f"清理会话失败: {e}")
|
|
|
+ # 即使清理失败,也要尝试释放连接
|
|
|
+ if self.doubao_client:
|
|
|
+ try:
|
|
|
+ await connection_pool.release(self.doubao_client)
|
|
|
+ self.doubao_client = None
|
|
|
+ logger.info(f"在异常处理中成功释放连接")
|
|
|
+ except Exception as release_error:
|
|
|
+ logger.error(f"释放连接失败: {release_error}")
|
|
|
|
|
|
|
|
|
class ConnectionManager:
|