drakken.rate_limit module

Rate limit module.

exception drakken.rate_limit.OverLimit

Bases: Exception

The number of requests exceeds the limit.

drakken.rate_limit.check_limit(limit, unit, name='default')

Increment count and compare to limit.

Parameters:
  • limit (int) – max number of executions.

  • unit (str) – ‘SECOND’, ‘MINUTE’, ‘HOUR’, ‘DAY’.

  • name (str) – limiter name. Default is ‘default’.

Raises:

OverLimit – number of calls is over the limit.

Uses a fixed window rate length algorithm:

  1. On the first call, store the datetime and the count (1).

  2. On subsequent calls, check the datetime: if the time window has expired, store the new datetime and reset the count to (1).

  3. If the window hasn’t expired, increment and store the count.

  4. If the count is over the limit, raise OverLimit exception.

Example:

from drakken.rate_limit import check_limit, OverLimit
limit = 12
unit = 'MINUTE'
try:
    check_limit(limit=limit, unit=unit)
except OverLimit as e:
    s = f'Rate limit exceeded: {limit} per {unit} count: {e}'
    logger.warning(s)
drakken.rate_limit.rlimit(limit, unit, name='default')

Rate limit decorator that calls check_limit().

Parameters:
  • limit (int) – max number of executions.

  • unit (str) – ‘SECOND’, ‘MINUTE’, ‘HOUR’, ‘DAY’.

  • name (str) – limiter name. Default is ‘default’.

If the rate limit is exceeded the decorated function isn’t executed.

Example:

import drakken.rate_limit as rlimit

@rlimit.rlimit(limit=3, unit='SECOND')
def sum(x, y):
    return x + y

z = sum(4, 3)   # 1
z = sum(12, 1)  # 2
z = sum(6, 2)   # 3
z = sum(9, 5)   # Over limit