fix: 修复连接池计数和任务调度器默认值问题
1. db_pool.py - 修复连接计数不一致问题 - 将 _created_connections 递增移到 put() 成功之后 - 确保 Full 异常和创建异常时正确关闭连接 - 避免计数器永久偏高 2. services/tasks.py - 统一 _running_by_user 默认值 - 将减少计数时的默认值从 1 改为 0 - 与增加计数时的默认值保持一致 - 添加注释说明 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
19
db_pool.py
19
db_pool.py
@@ -109,17 +109,26 @@ class ConnectionPool:
|
||||
with self._lock:
|
||||
# 双重检查:确保池确实需要补充
|
||||
if self._pool.qsize() < self.pool_size:
|
||||
new_conn = None
|
||||
try:
|
||||
new_conn = self._create_connection()
|
||||
self._created_connections += 1
|
||||
self._pool.put(new_conn, block=False)
|
||||
# 只有成功放入池后才增加计数
|
||||
self._created_connections += 1
|
||||
except Full:
|
||||
# 在获取锁期间池被填满了,关闭新建的连接
|
||||
try:
|
||||
new_conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
if new_conn:
|
||||
try:
|
||||
new_conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as create_error:
|
||||
# 创建连接失败,确保关闭已创建的连接
|
||||
if new_conn:
|
||||
try:
|
||||
new_conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
print(f"重建连接失败: {create_error}")
|
||||
|
||||
def close_all(self):
|
||||
|
||||
@@ -327,7 +327,8 @@ class TaskScheduler:
|
||||
except Exception:
|
||||
with self._cond:
|
||||
self._running_global = max(0, self._running_global - 1)
|
||||
self._running_by_user[task.user_id] = max(0, self._running_by_user.get(task.user_id, 1) - 1)
|
||||
# 使用默认值 0 与增加时保持一致
|
||||
self._running_by_user[task.user_id] = max(0, self._running_by_user.get(task.user_id, 0) - 1)
|
||||
if self._running_by_user.get(task.user_id) == 0:
|
||||
self._running_by_user.pop(task.user_id, None)
|
||||
self._cond.notify_all()
|
||||
@@ -385,7 +386,8 @@ class TaskScheduler:
|
||||
safe_remove_task(task.account_id)
|
||||
with self._cond:
|
||||
self._running_global = max(0, self._running_global - 1)
|
||||
self._running_by_user[task.user_id] = max(0, self._running_by_user.get(task.user_id, 1) - 1)
|
||||
# 使用默认值 0 与增加时保持一致
|
||||
self._running_by_user[task.user_id] = max(0, self._running_by_user.get(task.user_id, 0) - 1)
|
||||
if self._running_by_user.get(task.user_id) == 0:
|
||||
self._running_by_user.pop(task.user_id, None)
|
||||
self._cond.notify_all()
|
||||
|
||||
Reference in New Issue
Block a user