Skip to content

Commit 134e0b2

Browse files
committed
Allow specifying default ratelimit limit for advanced usage
1 parent 80af420 commit 134e0b2

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

discord/client.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,20 +327,17 @@ def __init__(self, **options: Any) -> None:
327327
self.ws: DiscordWebSocket = None # type: ignore
328328
self._listeners: Dict[str, List[Tuple[asyncio.Future, Callable[..., bool]]]] = {}
329329

330-
proxy: Optional[str] = options.pop('proxy', None)
331-
proxy_auth: Optional[aiohttp.BasicAuth] = options.pop('proxy_auth', None)
332-
unsync_clock: bool = options.pop('assume_unsync_clock', True)
333-
max_ratelimit_timeout: Optional[float] = options.pop('max_ratelimit_timeout', None)
334330
self.captcha_handler: Optional[Callable[[CaptchaRequired, Client], Awaitable[str]]] = options.pop(
335331
'captcha_handler', None
336332
)
337333
self.http: HTTPClient = HTTPClient(
338334
loop=self.loop,
339-
proxy=proxy,
340-
proxy_auth=proxy_auth,
341-
unsync_clock=unsync_clock,
335+
proxy=options.pop('proxy', None),
336+
proxy_auth=options.pop('proxy_auth', None),
337+
unsync_clock=options.pop('assume_unsync_clock', True),
342338
captcha=self.handle_captcha,
343-
max_ratelimit_timeout=max_ratelimit_timeout,
339+
max_ratelimit_timeout=options.pop('max_ratelimit_timeout', None),
340+
default_ratelimit_limit=options.pop('default_ratelimit_limit', None) or 1,
344341
locale=lambda: self._connection.locale,
345342
debug_options=self._get_debug_options(**options),
346343
rpc_proxy=options.pop('rpc_proxy', None),

discord/http.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,19 +442,21 @@ class Ratelimit:
442442
'dirty',
443443
'_last_request',
444444
'_max_ratelimit_timeout',
445+
'_default_ratelimit_limit',
445446
'_loop',
446447
'_pending_requests',
447448
'_sleeping',
448449
)
449450

450-
def __init__(self, max_ratelimit_timeout: Optional[float]) -> None:
451-
self.limit: int = 1
451+
def __init__(self, max_ratelimit_timeout: Optional[float], default_ratelimit_limit: int) -> None:
452+
self.limit: int = default_ratelimit_limit
452453
self.remaining: int = self.limit
453454
self.outgoing: int = 0
454455
self.reset_after: float = 0.0
455456
self.expires: Optional[float] = None
456457
self.dirty: bool = False
457458
self._max_ratelimit_timeout: Optional[float] = max_ratelimit_timeout
459+
self._default_ratelimit_limit: int = default_ratelimit_limit
458460
self._loop: asyncio.AbstractEventLoop = asyncio.get_running_loop()
459461
self._pending_requests: deque[asyncio.Future[Any]] = deque()
460462
# Only a single rate limit object should be sleeping at a time.
@@ -476,7 +478,7 @@ def reset(self):
476478

477479
def update(self, response: Union[aiohttp.ClientResponse, requests.Response], *, use_clock: bool = False) -> None:
478480
headers = response.headers
479-
self.limit = int(headers.get('X-Ratelimit-Limit', 1))
481+
self.limit = int(headers.get('X-Ratelimit-Limit', self._default_ratelimit_limit))
480482

481483
if self.dirty:
482484
self.remaining = min(int(headers.get('X-Ratelimit-Remaining', 0)), self.limit - self.outgoing)
@@ -603,6 +605,7 @@ def __init__(
603605
unsync_clock: bool = True,
604606
captcha: Optional[Callable[[CaptchaRequired], Coroutine[Any, Any, str]]] = None,
605607
max_ratelimit_timeout: Optional[float] = None,
608+
default_ratelimit_limit: int = 1,
606609
locale: Callable[[], str] = lambda: 'en-US',
607610
extra_headers: Optional[Mapping[str, str]] = None,
608611
debug_options: Optional[Sequence[str]] = None,
@@ -633,6 +636,7 @@ def __init__(
633636
self.use_clock: bool = not unsync_clock
634637
self.captcha_handler: Optional[Callable[[CaptchaRequired], Coroutine[Any, Any, str]]] = captcha
635638
self.max_ratelimit_timeout: Optional[float] = max(30.0, max_ratelimit_timeout) if max_ratelimit_timeout else None
639+
self.default_ratelimit_limit: int = default_ratelimit_limit
636640
self.get_locale: Callable[[], str] = locale
637641
self.extra_headers: Mapping[str, str] = extra_headers or {}
638642
self.debug_options: Optional[Sequence[str]] = debug_options
@@ -732,7 +736,7 @@ def get_ratelimit(self, key: str) -> Ratelimit:
732736
try:
733737
value = self._buckets[key]
734738
except KeyError:
735-
self._buckets[key] = value = Ratelimit(self.max_ratelimit_timeout)
739+
self._buckets[key] = value = Ratelimit(self.max_ratelimit_timeout, self.default_ratelimit_limit)
736740
self._try_clear_expired_ratelimits()
737741
return value
738742

0 commit comments

Comments
 (0)