mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-19 20:41:57 +00:00
出具、按需视图的每日上限改成先占额度再发起(用户拍板):新增 cluster/quota——额度记在库里(rate_counters 按名字 + 本地日期一行),占一份是一句带条件的 UPDATE(没到上限才加一),两种库上都是原子的,这一天第一次占按库里已有的记录起算,发起被拒或出错就退回。出具去掉全站一把锁,只留同一(运行、工作流、版本)的去重锁,几份不同的出具可以同时发起;按需视图的锁缩回按缓存键(视图 + 参数)去重,每日上限改用这个应用的额度。补测试:20 个协程同时占正好到上限、按已有记录起算、退回再占;两个进程同时占正好到上限;三份不同的答案同时出具正好到上限、多出来的回 429
This commit is contained in:
@@ -341,3 +341,37 @@ async def _until(check, timeout: float = 10.0) -> None:
|
||||
if loop.time() > deadline:
|
||||
raise AssertionError("等超时了")
|
||||
await asyncio.sleep(0.05)
|
||||
|
||||
|
||||
async def test_a_daily_quota_is_taken_up_to_the_limit_by_concurrent_callers():
|
||||
"""先占额度再发起(cluster/quota):20 个协程同时占、上限 5,正好 5 个占到;这一天第一次占按已有的记录起算;退回一份又能占。"""
|
||||
from app.cluster import quota
|
||||
|
||||
name = f"test-quota-{new_id()[:8]}"
|
||||
|
||||
async def seed(_session) -> int:
|
||||
return 2 # 今天已经做过 2 次
|
||||
|
||||
got = await asyncio.gather(*(quota.take(name, 5, seed=seed) for _ in range(20)))
|
||||
assert sum(got) == 3 and await quota.used(name) == 5
|
||||
assert not await quota.take(name, 5)
|
||||
await quota.give_back(name)
|
||||
assert await quota.take(name, 5) and not await quota.take(name, 5)
|
||||
|
||||
|
||||
def test_a_daily_quota_holds_across_two_processes():
|
||||
"""两个进程同时各占 15 次、上限 10:合计正好 10 次。"""
|
||||
name = f"test-quota-{new_id()[:8]}"
|
||||
backend = Path(__file__).resolve().parents[1]
|
||||
script = ("import asyncio, sys\n"
|
||||
"from app.cluster import quota\n"
|
||||
"async def main():\n"
|
||||
f" got = await asyncio.gather(*(quota.take({name!r}, 10) for _ in range(15)))\n"
|
||||
" print(sum(got))\n"
|
||||
"asyncio.run(main())\n")
|
||||
env = {**os.environ, "PYTHONPATH": str(backend), "AGENTLAB_DB_POOL_SIZE": "5"}
|
||||
procs = [subprocess.Popen([sys.executable, "-c", script], cwd=str(backend), env=env, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE, text=True) for _ in range(2)]
|
||||
outs = [p.communicate(timeout=120) for p in procs]
|
||||
assert all(p.returncode == 0 for p in procs), [o[1][-2000:] for o in outs]
|
||||
assert sum(int(o[0].strip().splitlines()[-1]) for o in outs) == 10
|
||||
|
||||
@@ -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