mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-25 21:22:18 +00:00
出具、按需视图的每日上限改成先占额度再发起(用户拍板):新增 cluster/quota——额度记在库里(rate_counters 按名字 + 本地日期一行),占一份是一句带条件的 UPDATE(没到上限才加一),两种库上都是原子的,这一天第一次占按库里已有的记录起算,发起被拒或出错就退回。出具去掉全站一把锁,只留同一(运行、工作流、版本)的去重锁,几份不同的出具可以同时发起;按需视图的锁缩回按缓存键(视图 + 参数)去重,每日上限改用这个应用的额度。补测试:20 个协程同时占正好到上限、按已有记录起算、退回再占;两个进程同时占正好到上限;三份不同的答案同时出具正好到上限、多出来的回 429
This commit is contained in:
@@ -297,6 +297,28 @@ async def test_users_issue_only_versions_pinned_by_apps_they_can_use(monkeypatch
|
||||
assert created and formal.started_by == "carol" and formal.origin["granted_by_app"] == board_id
|
||||
|
||||
|
||||
async def test_different_answers_issued_at_once_stop_exactly_at_the_daily_limit(monkeypatch):
|
||||
"""几份不同的答案同时出具(不再一把全站锁一个接一个):先在库里占今天的额度再发起,正好到上限为止,多出来的回 429。"""
|
||||
wf = await S.publish(level="published")
|
||||
rows = [await equivalent_run(monkeypatch) for _ in range(3)]
|
||||
from app.cluster import quota
|
||||
|
||||
async with SessionLocal() as session:
|
||||
issued = await ledger_issue.issued_today(session)
|
||||
# 今天的额度在库里记着(同一个测试进程里前面的测试也出具过);还没记过的按今天已出具的起算
|
||||
already = await quota.used(ledger_issue.QUOTA)
|
||||
already = issued if already is None else already
|
||||
await set_ledger(issue_daily_limit=max(already, issued) + 2)
|
||||
results = await asyncio.gather(*(ledger_issue.issue(r.id, wf, 1, principal=ADMIN) for r in rows),
|
||||
return_exceptions=True)
|
||||
made = [r for r in results if not isinstance(r, BaseException)]
|
||||
refused = [r for r in results if isinstance(r, ledger_issue.IssueRefused)]
|
||||
assert len(made) == 2 and all(created for _, created in made), results
|
||||
assert len(refused) == 1 and refused[0].status_code == 429 and refused[0].code == ledger_issue.DAILY_LIMIT
|
||||
async with SessionLocal() as session:
|
||||
assert await ledger_issue.issued_today(session) == issued + 2
|
||||
|
||||
|
||||
async def test_concurrent_requests_issue_once(monkeypatch):
|
||||
wf = await S.publish(level="published")
|
||||
row = await equivalent_run(monkeypatch)
|
||||
|
||||
Reference in New Issue
Block a user