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
Raises

TypeErrorrate parameter provided to the constructor is of invalid type.

rates

A tuple of Rate sorted in ascending order by the rate’s time period.

Type

tuple[Rate]

store

The store in use for this RateLimit.

Type

BaseStore

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.

async reset(key: Optional[uprate.ratelimit.H] = None) None[source]

Reset the given key.

Parameters

key (H, None, (H | None)) – The key to reset ratelimit for. If None, then resets all ratelimits, by default None.

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 a SyncMemoryStore is used. By default, None.

Raises

TypeErrorrate 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]

store

The sync store in use for this RateLimit.

Type

SyncStore