INNER CODE UNIT · Python
__init__
atenreiro/opensquat · opensquat/app.py:59
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
def wait(self):
if self._min_interval <= 0:
return
# Hold the lock while sleeping so slots are strictly serialized —
# releasing early would let multiple workers read the same next_slot
# value and pile up on identical timestamps, bursting past the limit.
with self._lock:
now = time.monotonic()
if now < self._next_slot:
time.sleep(self._next_slot - now)