fix: move rate limiter sleep outside lock to avoid blocking threads
Co-authored-by: aguzererler <6199053+aguzererler@users.noreply.github.com>
This commit is contained in:
parent
2193ff3fa1
commit
15e87c7688
|
|
@ -97,14 +97,18 @@ def _rate_limited_request(function_name: str, params: dict, timeout: int = 30) -
|
|||
|
||||
# Re-check and register under lock to avoid races where multiple
|
||||
# threads calculate similar sleep times and then all fire at once.
|
||||
with _rate_lock:
|
||||
now = _time.time()
|
||||
_call_timestamps[:] = [t for t in _call_timestamps if now - t < 60]
|
||||
if len(_call_timestamps) >= _RATE_LIMIT:
|
||||
# Another thread filled the window while we slept — wait again
|
||||
extra_sleep = 60 - (now - _call_timestamps[0]) + 0.1
|
||||
_time.sleep(extra_sleep)
|
||||
_call_timestamps.append(_time.time())
|
||||
while True:
|
||||
with _rate_lock:
|
||||
now = _time.time()
|
||||
_call_timestamps[:] = [t for t in _call_timestamps if now - t < 60]
|
||||
if len(_call_timestamps) >= _RATE_LIMIT:
|
||||
# Another thread filled the window while we slept — wait again
|
||||
extra_sleep = 60 - (now - _call_timestamps[0]) + 0.1
|
||||
else:
|
||||
_call_timestamps.append(_time.time())
|
||||
break
|
||||
# Sleep outside the lock to avoid blocking other threads
|
||||
_time.sleep(extra_sleep)
|
||||
|
||||
|
||||
return _make_api_request(function_name, params, timeout=timeout)
|
||||
|
|
|
|||
Loading…
Reference in New Issue