pyrate_limiter.buckets.postgres module#

A bucket using PostgreSQL as backend

class pyrate_limiter.buckets.postgres.PostgresBucket(pool, table, rates)#

Bases: pyrate_limiter.abstracts.bucket.AbstractBucket

count()#

Count number of items in the bucket

Return type

Union[int, Awaitable[int]]

flush()#

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

Return type

Optional[Awaitable[None]]

leak(current_timestamp=None)#

leaking bucket - removing items that are outdated

Return type

Union[int, Awaitable[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

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

pool#
put(item)#

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

Return type

Union[bool, Awaitable[bool]]

table#
class pyrate_limiter.buckets.postgres.Queries#

Bases: object

COUNT = '\n    SELECT COUNT(*) FROM {table}\n    '#
CREATE_BUCKET_TABLE = '\n    CREATE TABLE IF NOT EXISTS {table} (\n        name VARCHAR,\n        weight SMALLINT,\n        item_timestamp TIMESTAMP\n    )\n    '#
CREATE_INDEX_ON_TIMESTAMP = '\n    CREATE INDEX IF NOT EXISTS {index} ON {table} (item_timestamp)\n    '#
FLUSH = '\n    DELETE FROM {table}\n    '#
LEAK = '\n    DELETE FROM {table} WHERE item_timestamp < TO_TIMESTAMP({timestamp})\n    '#
LEAK_COUNT = '\n    SELECT COUNT(*) FROM {table} WHERE item_timestamp < TO_TIMESTAMP({timestamp})\n    '#
PEEK = '\n    SELECT name, weight, (extract(EPOCH FROM item_timestamp) * 1000) as item_timestamp\n    FROM {table}\n    ORDER BY item_timestamp DESC\n    LIMIT 1\n    OFFSET {offset}\n    '#
PUT = '\n    INSERT INTO {table} (name, weight, item_timestamp) VALUES (%s, %s, TO_TIMESTAMP(%s))\n    '#