pyrate_limiter.abstracts.bucket module#

Implement this class to create a workable bucket for Limiter to use

class pyrate_limiter.abstracts.bucket.AbstractBucket#

Bases: abc.ABC

Base bucket interface Assumption: len(rates) always > 0 TODO: allow empty rates

abstract count()#

Count number of items in the bucket

Return type

Union[int, Awaitable[int]]

failing_rate = None#
abstract flush()#

Flush the whole bucket - Must remove failing-rate after flushing

Return type

Optional[Awaitable[None]]

abstract leak(current_timestamp=None)#

leaking bucket - removing items that are outdated

Return type

Union[int, Awaitable[int]]

abstract peek(index)#

Peek at the rate-item at a specific index in latest-to-earliest order NOTE: The reason we cannot peek from the start of the queue(earliest-to-latest) is we can’t really tell how many outdated items are still in the queue

Return type

Union[RateItem, None, Awaitable[Optional[RateItem]]]

abstract put(item)#

Put an item (typically the current time) in the bucket return true if successful, otherwise false

Return type

Union[bool, Awaitable[bool]]

rates#
waiting(item)#

Calculate time until bucket become availabe to consume an item again

Return type

Union[int, Awaitable[int]]

class pyrate_limiter.abstracts.bucket.BucketAsyncWrapper(bucket)#

Bases: pyrate_limiter.abstracts.bucket.AbstractBucket

BucketAsyncWrapper is a wrapping over any bucket that turns a async/synchronous bucket into an async one

async count()#

Count number of items in the bucket

property failing_rate#
async flush()#

Flush the whole bucket - Must remove failing-rate after flushing

Return type

None

async leak(current_timestamp=None)#

leaking bucket - removing items that are outdated

Return type

int

async peek(index)#

Peek at the rate-item at a specific index in latest-to-earliest order NOTE: The reason we cannot peek from the start of the queue(earliest-to-latest) is we can’t really tell how many outdated items are still in the queue

Return type

Optional[RateItem]

async put(item)#

Put an item (typically the current time) in the bucket return true if successful, otherwise false

property rates#
async waiting(item)#

Calculate time until bucket become availabe to consume an item again

Return type

int

class pyrate_limiter.abstracts.bucket.BucketFactory#

Bases: abc.ABC

Asbtract BucketFactory class. It is reserved for user to implement/override this class with his own bucket-routing/creating logic

create(clock, bucket_class, *args, **kwargs)#

Creating a bucket dynamically

Return type

AbstractBucket

abstract get(item)#

Get the corresponding bucket to this item

Return type

AbstractBucket

property leak_interval#

Retrieve leak-interval from inner Leaker task

Return type

int

schedule_leak(new_bucket, associated_clock)#

Schedule all the buckets’ leak, reset bucket’s failing rate

Return type

None

abstract wrap_item(name, weight=1)#

Add the current timestamp to the receiving item using any clock backend - Turn it into a RateItem - Can return either a coroutine or a RateItem instance

Return type

Union[RateItem, Awaitable[RateItem]]

class pyrate_limiter.abstracts.bucket.Leaker(leak_interval)#

Bases: threading.Thread

Responsible for scheduling buckets’ leaking at the background either through a daemon task(for sync buckets) or a task using asyncio.Task

async_buckets = None#
clocks = None#
daemon = True#
is_async_leak_started = False#
leak_async()#
leak_interval = 10000#
name = "PyrateLimiter's Leaker"#
register(bucket, clock)#

Register a new bucket with its associated clock

run()#

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

Return type

None

start()#

Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

Return type

None

sync_buckets = None#