116 lines
3.6 KiB
Python
116 lines
3.6 KiB
Python
import asyncio
|
|
import multiprocessing as mp
|
|
import time
|
|
|
|
from pipewright.models.base import rate_limiter_from
|
|
from pipewright.scheduler import RateLimiter, SharedLedger
|
|
|
|
|
|
class FakeClock:
|
|
def __init__(self, t: float = 1_000_000.0):
|
|
self.t = t
|
|
|
|
def __call__(self) -> float:
|
|
return self.t
|
|
|
|
|
|
def _pair(tmp_path, clock=time.time, **kw):
|
|
path = tmp_path / "k.sqlite"
|
|
a = RateLimiter(shared=SharedLedger(path, clock=clock, max_poll=0.05), **kw)
|
|
b = RateLimiter(shared=SharedLedger(path, clock=clock, max_poll=0.05), **kw)
|
|
return a, b
|
|
|
|
|
|
async def _try(lim: RateLimiter, timeout: float = 0.15):
|
|
try:
|
|
return await asyncio.wait_for(lim.acquire(10), timeout)
|
|
except asyncio.TimeoutError:
|
|
return None
|
|
|
|
|
|
def test_two_limiters_share_one_window(tmp_path):
|
|
clock = FakeClock()
|
|
a, b = _pair(tmp_path, clock, requests_per_minute=3)
|
|
|
|
async def main():
|
|
got = [await _try(a), await _try(b), await _try(a)]
|
|
assert all(got)
|
|
assert await _try(b) is None and await _try(a) is None # window full for both
|
|
clock.t += 61 # window rolls
|
|
assert await _try(b) is not None
|
|
|
|
asyncio.run(main())
|
|
assert a.snapshot()["in_window"] == 1 and b.stats["admitted"] == 2
|
|
|
|
|
|
def test_gate_set_by_one_blocks_the_other(tmp_path):
|
|
a, b = _pair(tmp_path, requests_per_minute=100)
|
|
|
|
async def main():
|
|
await a.acquire(1)
|
|
a.reject(retry_after=0.3)
|
|
t0 = time.monotonic()
|
|
await b.acquire(1)
|
|
return time.monotonic() - t0
|
|
|
|
assert asyncio.run(main()) >= 0.25
|
|
assert b.snapshot()["in_window"] == 2 # the rejected request still counts
|
|
|
|
|
|
def test_settle_updates_shared_totals(tmp_path):
|
|
clock = FakeClock()
|
|
a, b = _pair(tmp_path, clock, output_tokens_per_minute=1000, initial_output_estimate=400)
|
|
|
|
async def main():
|
|
x = await _try(a)
|
|
y = await _try(b)
|
|
assert await _try(a) is None # 400 + 400 + 400 > 1000
|
|
x.settle(10)
|
|
y.settle(20)
|
|
assert a.shared.totals(a.window)["output_tokens"] == 30
|
|
assert await _try(b) is not None
|
|
|
|
asyncio.run(main())
|
|
|
|
|
|
def test_config_shared_key_and_dir(tmp_path):
|
|
lim = rate_limiter_from({"requests_per_minute": 5, "shared": True, "shared_dir": "rate"}, "morph", tmp_path)
|
|
assert lim.shared.path == tmp_path / "rate" / "morph.sqlite"
|
|
lim = rate_limiter_from({"shared": "team/key"}, "morph")
|
|
assert lim.shared.path.name == "team_key.sqlite"
|
|
assert rate_limiter_from({"requests_per_minute": 5}, "morph").shared is None
|
|
|
|
|
|
def _worker(path: str, barrier, q) -> None:
|
|
async def main():
|
|
lim = RateLimiter(requests_per_minute=4, window=2.0, shared=SharedLedger(path, max_poll=0.02))
|
|
lim.shared.totals(1.0) # open + create the ledger before the race starts
|
|
barrier.wait(10)
|
|
stop = time.time() + 1.0 # well inside the 2 s window: nothing expires
|
|
n = 0
|
|
while time.time() < stop:
|
|
try:
|
|
await asyncio.wait_for(lim.acquire(1), max(0.01, stop - time.time()))
|
|
n += 1
|
|
except asyncio.TimeoutError:
|
|
break
|
|
return n
|
|
|
|
q.put(asyncio.run(main()))
|
|
|
|
|
|
def test_multiprocess_joint_admission(tmp_path):
|
|
ctx = mp.get_context("spawn")
|
|
q, barrier = ctx.Queue(), ctx.Barrier(3)
|
|
procs = [ctx.Process(target=_worker, args=(str(tmp_path / "rate.sqlite"), barrier, q)) for _ in range(3)]
|
|
for p in procs:
|
|
p.start()
|
|
try:
|
|
counts = [q.get(timeout=15) for _ in procs]
|
|
finally:
|
|
for p in procs:
|
|
p.join(5)
|
|
if p.is_alive():
|
|
p.kill()
|
|
assert sum(counts) == 4, counts
|