Ratelimit¶
Contains the implementiation for ratelimit related objects.
- class uprate.ratelimit.RateLimit(rate: Union[uprate.rate.Rate, uprate.rate.RateGroup], store: Optional[uprate.store.BaseStore[uprate.ratelimit.H]] = None)[source]¶
Enforces multiple rates per provided keys. This is a low-level component.
- Parameters
rate (
Rate
,RateGroup
, (Rate | RateGroup
)) – The rate(s) to enforce on keys in this ratelimits.store (
BaseStore
, (BaseStore | None
)) – The store to use for this RateLimit, If None then auprate.store.MemoryStore
is used. By default,None
.
- Raises
TypeError –
rate
parameter provided to the constructor is of invalid type.
- async acquire(key: uprate.ratelimit.H) None [source]¶
Try to acquire a usage token for given token. Raise an error if token can’t be acquired.
- Parameters
key (
H
) – The key to acquire a usage token for.- Raises
RateLimitError – Cannot acquire usage token due to exhaustion.
Sync Ratelimit¶
- class uprate._sync.SyncRateLimit(rate: Rate | RateGroup, store: SyncStore[G] | None = None)[source]¶
Enforces multiple rates per provided keys. This is a low-level sync component. Sync version of
uprate.ratelimit.RateLimit
- Parameters
rate (
Rate
,RateGroup
, (Rate | RateGroup
)) – The rate(s) to enforce on keys in this ratelimits.store (
SyncStore
, (SyncStore | None
)) – The sync store to use for this SyncRateLimit, If None then aSyncMemoryStore
is used. By default,None
.
- Raises
TypeError –
rate
parameter provided to the constructor is of invalid type.
- rates¶
A tuple of
uprate.rate.Rate
sorted in ascending order by the rate’s time period.- Type
tuple[
uprate.rate.Rate
]