Skip to content

Commit

Permalink
Fix cache eviction for ratelimit objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapptz committed Aug 17, 2022
1 parent 54ee383 commit 8dd186c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ class Ratelimit:
'reset_after',
'expires',
'dirty',
'_last_request',
'_max_ratelimit_timeout',
'_loop',
'_pending_requests',
Expand All @@ -355,6 +356,7 @@ def __init__(self, max_ratelimit_timeout: Optional[float]) -> None:
# The object that is sleeping is ultimately responsible for freeing the semaphore
# for the requests currently pending.
self._sleeping: asyncio.Lock = asyncio.Lock()
self._last_request: float = self._loop.time()

def __repr__(self) -> str:
return (
Expand Down Expand Up @@ -422,7 +424,12 @@ async def _refresh(self) -> None:
def is_expired(self) -> bool:
return self.expires is not None and self._loop.time() > self.expires

def is_inactive(self) -> bool:
delta = self._loop.time() - self._last_request
return delta >= 300 and self.outgoing == 0 and len(self._pending_requests) == 0

async def acquire(self) -> None:
self._last_request = self._loop.time()
if self.is_expired():
self.reset()

Expand Down Expand Up @@ -532,7 +539,7 @@ def _try_clear_expired_ratelimits(self) -> None:
if len(self._buckets) < 256:
return

keys = [key for key, bucket in self._buckets.items() if bucket.is_expired()]
keys = [key for key, bucket in self._buckets.items() if bucket.is_inactive()]
for key in keys:
del self._buckets[key]

Expand Down

0 comments on commit 8dd186c

Please sign in to comment.