INNER CODE UNIT · Python
_RateLimiter
atenreiro/opensquat · opensquat/app.py:48
class _RateLimiter:
"""
Thread-safe fixed-interval rate limiter shared across worker threads.
When rate_per_sec is None or <= 0, wait() is a no-op (zero overhead).
Otherwise, wait() blocks each caller until its assigned time slot,
guaranteeing at most rate_per_sec requests per second across all
callers. Slots are serialized under a single lock so concurrent
workers cannot burst past the limit.
"""
def __init__(self, rate_per_sec):
if rate_per_sec and rate_per_sec > 0:
self._min_interval = 1.0 / rate_per_sec
else:
self._min_interval = 0.0
self._lock = threading.Lock()
self._next_slot = 0.0