From 8dd186cf1ee108d2ce8c5d4d099929a59173942d Mon Sep 17 00:00:00 2001 From: Rapptz Date: Wed, 17 Aug 2022 08:39:39 -0400 Subject: [PATCH] Fix cache eviction for ratelimit objects --- discord/http.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/discord/http.py b/discord/http.py index 85453baf5..e72b6225d 100644 --- a/discord/http.py +++ b/discord/http.py @@ -335,6 +335,7 @@ class Ratelimit: 'reset_after', 'expires', 'dirty', + '_last_request', '_max_ratelimit_timeout', '_loop', '_pending_requests', @@ -355,6 +356,7 @@ class Ratelimit: # 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 ( @@ -422,7 +424,12 @@ class Ratelimit: 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() @@ -532,7 +539,7 @@ class HTTPClient: 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]