pyrate_limiter.bucket module#

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

class pyrate_limiter.bucket.AbstractBucket(maxsize=0, **_kwargs)#

Bases: abc.ABC

Base bucket interface

abstract all_items()#

Return a list as copies of all items in the bucket

Return type

List[float]

abstract flush()#

Flush/reset bucket

Return type

None

abstract get(number)#

Get items, remove them from the bucket in the FIFO order, and return the number of items that have been removed

Return type

int

inspect_expired_items(time)#

Find how many items in bucket that have slipped out of the time-window

Return type

Tuple[int, float]

Returns

The number of unexpired items, and the time until the next item will expire

lock_acquire()#

Acquire a lock prior to beginning a new transaction, if needed

lock_release()#

Release lock following a transaction, if needed

maxsize()#

Return the maximum size of the bucket, ie the maximum number of item this bucket can hold

Return type

int

abstract put(item)#

Put an item (typically the current time) in the bucket Return 1 if successful, else 0

Return type

int

abstract size()#

Return the current size of the bucket, ie the count of all items currently in the bucket

Return type

int

class pyrate_limiter.bucket.MemoryListBucket(maxsize=0, **_kwargs)#

Bases: pyrate_limiter.bucket.AbstractBucket

A bucket that resides in memory using python’s List

all_items()#

Return a list as copies of all items in the bucket

Return type

List[float]

flush()#

Flush/reset bucket

get(number)#

Get items, remove them from the bucket in the FIFO order, and return the number of items that have been removed

Return type

int

put(item)#

Put an item (typically the current time) in the bucket Return 1 if successful, else 0

size()#

Return the current size of the bucket, ie the count of all items currently in the bucket

Return type

int

class pyrate_limiter.bucket.MemoryQueueBucket(maxsize=0, **_kwargs)#

Bases: pyrate_limiter.bucket.AbstractBucket

A bucket that resides in memory using python’s built-in Queue class

all_items()#

Return a list as copies of all items in the bucket

Return type

List[float]

flush()#

Flush/reset bucket

get(number)#

Get items, remove them from the bucket in the FIFO order, and return the number of items that have been removed

Return type

int

put(item)#

Put an item (typically the current time) in the bucket Return 1 if successful, else 0

size()#

Return the current size of the bucket, ie the count of all items currently in the bucket

Return type

int

class pyrate_limiter.bucket.RedisBucket(maxsize=0, redis_pool=None, bucket_name=None, identity=None, expire_time=None, **_kwargs)#

Bases: pyrate_limiter.bucket.AbstractBucket

A bucket backed by a Redis instance

all_items()#

Return a list as copies of all items in the bucket

Return type

List[float]

flush()#

Flush/reset bucket

get(number)#

Get items, remove them from the bucket in the FIFO order, and return the number of items that have been removed

Return type

int

get_connection()#

Obtain a connection from redis pool

get_pipeline()#

Using redis pipeline for batch operation

put(item)#

Put an item (typically the current time) in the bucket Return 1 if successful, else 0

size()#

Return the current size of the bucket, ie the count of all items currently in the bucket

Return type

int

class pyrate_limiter.bucket.RedisClusterBucket(maxsize=0, redis_pool=None, bucket_name=None, identity=None, expire_time=None, **_kwargs)#

Bases: pyrate_limiter.bucket.RedisBucket

A bucket backed by a Redis cluster

get_connection()#

Obtain a connection from redis pool