Limiters#
This module contains all the built-in limiter hooks contained in arc
.
Note
Any object that implements the LimiterProto
protocol is a valid limiter. If you are looking to create your custom ratelimiter implementation, you should start there.
LimiterHook
#
Bases: RateLimiter[Context[ClientT]]
, LimiterProto[ClientT]
The default implementation of a ratelimiter that can be used as a hook.
PARAMETER | DESCRIPTION |
---|---|
period |
The period, in seconds, after which the bucket resets.
TYPE:
|
limit |
The amount of requests allowed in a bucket.
TYPE:
|
get_key_with |
A callable that returns a key for the ratelimiter bucket. |
__call__
async
#
__call__(item: KeyT) -> HookResult
Acquire a ratelimit, fail if ratelimited.
PARAMETER | DESCRIPTION |
---|---|
item |
The context to evaluate the ratelimit under.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
HookResult
|
A hook result to conform to the limiter protocol. |
RAISES | DESCRIPTION |
---|---|
UnderCooldownError
|
If the ratelimiter is ratelimited. |
acquire
async
#
Acquire a bucket, block execution if ratelimited and wait is True.
PARAMETER | DESCRIPTION |
---|---|
ctx |
The context to evaluate the ratelimit under. |
wait |
Determines if this call should block in case of hitting a ratelimit.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
UnderCooldownError
|
If the ratelimiter is ratelimited and wait is False. |
channel_limiter
#
channel_limiter(period: float, limit: int) -> LimiterHook[Any]
Create a channel ratelimiter.
This ratelimiter is shared across all contexts in a channel.
PARAMETER | DESCRIPTION |
---|---|
period |
The period, in seconds, after which the bucket resets.
TYPE:
|
limit |
The amount of requests allowed in a bucket.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
RateLimiterExhaustedError
|
If the limiter is exhausted. |
custom_limiter
#
custom_limiter(
period: float, limit: int, get_key_with: Callable[[Context[Any]], str]
) -> LimiterHook[Any]
Create a ratelimiter with a custom key extraction function.
PARAMETER | DESCRIPTION |
---|---|
period |
The period, in seconds, after which the bucket resets.
TYPE:
|
limit |
The amount of requests allowed in a bucket.
TYPE:
|
get_key_with |
A callable that returns a key for the ratelimiter bucket. This key is used to identify the bucket.
For instance, to create a ratelimiter that is shared across all contexts in a guild,
you would use |
RAISES | DESCRIPTION |
---|---|
RateLimiterExhaustedError
|
If the limiter is exhausted. |
global_limiter
#
global_limiter(period: float, limit: int) -> LimiterHook[Any]
Create a global ratelimiter.
This ratelimiter is shared across all invocation contexts.
PARAMETER | DESCRIPTION |
---|---|
period |
The period, in seconds, after which the bucket resets.
TYPE:
|
limit |
The amount of requests allowed in a bucket.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
RateLimiterExhaustedError
|
If the limiter is exhausted. |
guild_limiter
#
guild_limiter(period: float, limit: int) -> LimiterHook[Any]
Create a guild ratelimiter.
This ratelimiter is shared across all contexts in a guild.
PARAMETER | DESCRIPTION |
---|---|
period |
The period, in seconds, after which the bucket resets.
TYPE:
|
limit |
The amount of requests allowed in a bucket.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
RateLimiterExhaustedError
|
If the limiter is exhausted. |
member_limiter
#
member_limiter(period: float, limit: int) -> LimiterHook[Any]
Create a member ratelimiter.
This ratelimiter is shared across all contexts by a member in a guild. The same user in a different guild will be assigned a different bucket.
PARAMETER | DESCRIPTION |
---|---|
period |
The period, in seconds, after which the bucket resets.
TYPE:
|
limit |
The amount of requests allowed in a bucket.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
RateLimiterExhaustedError
|
If the limiter is exhausted. |
user_limiter
#
user_limiter(period: float, limit: int) -> LimiterHook[Any]
Create a user ratelimiter.
This ratelimiter is shared across all contexts by a user.
PARAMETER | DESCRIPTION |
---|---|
period |
The period, in seconds, after which the bucket resets.
TYPE:
|
limit |
The amount of requests allowed in a bucket.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
RateLimiterExhaustedError
|
If the limiter is exhausted. |