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
- failing_rate = None#
- abstract flush()#
Flush the whole bucket - Must remove failing-rate after flushing
- abstract leak(current_timestamp=None)#
leaking bucket - removing items that are outdated
- 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
- abstract put(item)#
Put an item (typically the current time) in the bucket return true if successful, otherwise false
- rates#
- 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 leak(current_timestamp=None)#
leaking bucket - removing items that are outdated
- Return type
- 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
- async put(item)#
Put an item (typically the current time) in the bucket return true if successful, otherwise false
- property rates#
- 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
- abstract get(item)#
Get the corresponding bucket to this item
- Return type
- schedule_leak(new_bucket, associated_clock)#
Schedule all the buckets’ leak, reset bucket’s failing rate
- Return type
- 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
- 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
- sync_buckets = None#