Files
nix-config-ryan4yin/agents/evals/review-comments-and-docs/fixtures/F12-ratelimit.py
T
ryan4yin | 二花 f32d67a5cc feat(agents): add review-comments-and-docs and two evaluation suites (#434)
Adds the review-comments-and-docs skill (1282-word body) and two evaluation suites: a 21-case
measured suite for the skill, and a harness that runs the two concurrency scenarios previously left
unrun in agents/evals/global-rules.md.
2026-10-12 00:35:06 +08:00

37 lines
1.0 KiB
Python

# SPDX-License-Identifier: MIT
# Copyright (c) 2026 Edge Systems
"""Token-bucket rate limiter for the delivery dispatcher."""
from time import monotonic
# 200 tokens is two seconds of refill at REFILL_PER_SECOND = 100.
BUCKET_CAPACITY = 200
REFILL_PER_SECOND = 100
class RateLimiter:
"""Fixed-rate token bucket.
Not thread-safe by design: the dispatcher has a single consumer.
Injected now values must be non-decreasing monotonic timestamps in seconds.
"""
def __init__(self):
self._tokens = float(BUCKET_CAPACITY)
self._updated = monotonic()
def allow(self, now=None):
# Wall-clock corrections must not change refill time.
# https://docs.python.org/3/library/time.html#time.monotonic
now = monotonic() if now is None else now
self._tokens = min(
BUCKET_CAPACITY,
self._tokens + (now - self._updated) * REFILL_PER_SECOND,
)
self._updated = now
if self._tokens >= 1.0:
self._tokens -= 1.0
return True
return False