From fa5fc4efcd9c6208eb7099fef4778bf1d7bcf0cd Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Fri, 20 Dec 2024 04:38:39 -0400 Subject: [PATCH] fix: change rate limit to slow drippy instead of bursty (#343) * fix: change rate limit to slow drippy instead of bursty * chore: `black .` * fix: make rate limit a slow drip instead of bursty * Update _session.py --------- Co-authored-by: github-actions[bot] --- dank_mids/_requests.py | 4 +--- dank_mids/helpers/_session.py | 12 ++++-------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/dank_mids/_requests.py b/dank_mids/_requests.py index 7c2e6f36..8ef5eadb 100644 --- a/dank_mids/_requests.py +++ b/dank_mids/_requests.py @@ -635,9 +635,7 @@ def should_retry(self, e: Exception) -> bool: _log_debug("Dank too loud. Bisecting batch and retrying.") elif isinstance(e, BadResponse) and (_needs_full_request_spec(e) or _is_call_revert(e)): pass - elif "429" not in str_e and all( - err not in str_e for err in constants.TOO_MUCH_DATA_ERRS - ): + elif "429" not in str_e and all(err not in str_e for err in constants.TOO_MUCH_DATA_ERRS): _log_warning("unexpected %s: %s", e.__class__.__name__, e) return len(self) > 1 diff --git a/dank_mids/helpers/_session.py b/dank_mids/helpers/_session.py index 6f99c272..4ba1e6b6 100644 --- a/dank_mids/helpers/_session.py +++ b/dank_mids/helpers/_session.py @@ -104,9 +104,8 @@ def __new__(cls, value, phrase, description=""): HTTPStatusExtended.CLOUDFLARE_TIMEOUT, # type: ignore [attr-defined] } - # default is 50 requests/second -limiters = defaultdict(lambda: AsyncLimiter(ENVS.REQUESTS_PER_SECOND, 1)) +limiters = defaultdict(lambda: AsyncLimiter(5, 5 / ENVS.REQUESTS_PER_SECOND)) _rate_limit_waiters = {} @@ -228,6 +227,7 @@ async def handle_too_many_requests(self, endpoint: str, error: ClientResponseErr if (now := time()) > getattr(limiter, "_last_updated_at", 0) + 10: current_rate = limiter._rate_per_sec new_rate = current_rate * 0.97 + limiter.time_period /= 0.97 limiter._rate_per_sec = new_rate limiter._last_updated_at = now _logger_info( @@ -251,12 +251,8 @@ async def handle_too_many_requests(self, endpoint: str, error: ClientResponseErr self._log_rate_limited(retry_after) if retry_after > 30: _logger_warning("severe rate limiting from your provider") - acquire_capacity_for_x_requests = retry_after / secs_between_requests - while acquire_capacity_for_x_requests: - # the limiter does this check that we need to work around - get_now = min(acquire_capacity_for_x_requests, limiter.max_rate) - await limiter.acquire(get_now) - acquire_capacity_for_x_requests -= get_now + # the limiter handles the timing + await limiter.acquire(5) def _log_rate_limited(self, try_after: float) -> None: if not self._limited: