fix(worker): 优化连接池容量拆分、并发入队与事件写入

This commit is contained in:
yml
2026-11-29 11:24:35 +08:00
parent 1d92fa6d53
commit 2ea105a924
18 changed files with 956 additions and 207 deletions
+77
View File
@@ -536,3 +536,80 @@ def test_login_quota_configuration_route_updates_all_categories(db):
assert updated["relogin_high_value_daily_limit"] == 240
assert updated["failure_pause_ratio"] == 35
assert loaded["login_concurrency"] == 3
def test_enqueue_account_work_survives_repeatable_read_integrity_race(db):
from tests.support.db import make_test_session
account = _account("queue-race-account")
db.add(account)
db.commit()
# 先开启 db 的 REPEATABLE READ 快照,再由另一会话提交同一工作项
assert db.query(AccountWorkItem).count() == 0
other = make_test_session()
try:
committed = enqueue_account_work(
other,
account_id=account.id,
work_type="storehome_sync",
priority=20,
)
other.commit()
committed_id = int(committed.id)
finally:
other.close()
# db 的旧快照看不到 committed,INSERT 会撞唯一键,但不能抛 NoResultFound
reused = enqueue_account_work(
db,
account_id=account.id,
work_type="storehome_sync",
priority=10,
)
assert int(reused.id) == committed_id
db.commit()
# 如果并发项在另一会话中刚被完成并清空 active_dedupe_key,再次撞锁时也能重新入队
other = make_test_session()
try:
current = other.get(AccountWorkItem, committed_id)
assert current is not None
complete_account_work(other, current)
other.commit()
finally:
other.close()
# 人工模拟同一快照里先误判存在冲突后被完成:重新入队应得到一条新记录
fresh = enqueue_account_work(
db,
account_id=account.id,
work_type="storehome_sync",
priority=10,
)
db.commit()
assert int(fresh.id) != committed_id
def test_enqueue_account_work_raises_integrity_error_when_retries_exhausted(
db, monkeypatch
):
import pytest
from sqlalchemy.exc import IntegrityError
account = _account("queue-race-exhausted")
db.add(account)
db.commit()
def always_conflict() -> None:
raise IntegrityError("INSERT", {}, Exception("Duplicate entry"))
monkeypatch.setattr(db, "flush", always_conflict)
with pytest.raises(IntegrityError):
enqueue_account_work(
db,
account_id=account.id,
work_type="storehome_sync",
priority=10,
)