pyrate_limiter.limiter module#

class pyrate_limiter.limiter.Limiter(*rates, bucket_class=<class 'pyrate_limiter.bucket.MemoryQueueBucket'>, bucket_kwargs=None, time_function=None)#

Bases: object

Main rate-limiter class

Parameters
  • rates (RequestRate) – Request rate definitions

  • bucket_class (Type[AbstractBucket]) – Bucket backend to use; may be any subclass of AbstractBucket. See :py:mod`pyrate_limiter.bucket` for available bucket classes.

  • bucket_kwargs (Optional[Dict[str, Any]]) – Extra keyword arguments to pass to the bucket class constructor.

  • time_function (Optional[Callable[[], float]]) – Time function that returns the current time as a float, in seconds

flush_all()#
Return type

int

get_current_volume(identity)#

Get current bucket volume for a specific identity

Return type

int

ratelimit(*identities, delay=False, max_delay=None)#

A decorator and contextmanager that applies rate-limiting, with async support. Depending on arguments, calls that exceed the rate limit will either raise an exception, or sleep until space is available in the bucket.

Parameters
  • identities (str) – One or more identities to acquire. Typically this is the name of a service or resource that is being rate-limited.

  • delay (bool) – Delay until the next request instead of raising an exception

  • max_delay (Union[int, float, None]) – The maximum allowed delay time (in seconds); anything over this will raise an exception

Raises

BucketFullException – If the rate limit is reached, and delay=False or the delay exceeds max_delay

try_acquire(*identities)#

Attempt to acquire an item, or raise an error if a rate limit has been exceeded.

Parameters

identities (str) – One or more identities to acquire. Typically this is the name of a service or resource that is being rate-limited.

Raises

BucketFullException – If the bucket is full and the item cannot be acquired

Return type

None