From aa2401c90d8a21422d14299c52be2e96d11c89b1 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Fri, 20 Dec 2024 00:51:10 -0400 Subject: [PATCH] feat: optimize rate limiting logic --- dank_mids/helpers/_session.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dank_mids/helpers/_session.py b/dank_mids/helpers/_session.py index e8461008..d3c12b4f 100644 --- a/dank_mids/helpers/_session.py +++ b/dank_mids/helpers/_session.py @@ -226,7 +226,7 @@ async def post(self, endpoint: str, *args, loads: JSONDecoder = DEFAULT_JSON_DEC async def handle_too_many_requests(self, endpoint: str, error: ClientResponseError) -> None: limiter = limiters[endpoint] - if (now := time()) > getattr(limiter, "_last_updated_at", 0) + 30: + if (now := time()) > getattr(limiter, "_last_updated_at", 0) + 10: current_rate = limiter._rate_per_sec new_rate = current_rate * 0.97 limiter._rate_per_sec = new_rate @@ -252,7 +252,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") - await sleep(retry_after) + acquire_capacity_for_x_requests = retry_after / secs_between_requests + await limiter.acquire(acquire_capacity_for_x_requests) def _log_rate_limited(self, try_after: float) -> None: if not self._limited: