Skip to content

Commit

Permalink
feat: optimize rate limiting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored Dec 20, 2024
1 parent 5f61d2f commit aa2401c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions dank_mids/helpers/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit aa2401c

Please sign in to comment.