pyrate_limiter.buckets.in_memory_bucket module#

Naive bucket implementation using built-in list

class pyrate_limiter.buckets.in_memory_bucket.InMemoryBucket(rates)#

Bases: pyrate_limiter.abstracts.bucket.AbstractBucket

Simple In-memory Bucket using native list Clock can be either time.time or time.monotonic When leak, clock is required Pros: fast, safe, and precise Cons: since it resides in local memory, the data is not persistent, nor scalable Usecase: small applications, simple logic

count()#

Count number of items in the bucket

Return type

int

flush()#

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

Return type

None

items#
leak(current_timestamp=None)#

leaking bucket - removing items that are outdated

Return type

int

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]

put(item)#

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

Return type

bool