API¶
Storage¶
-
class
limits.storage.Storage(uri=None, **options)[source]¶ Bases:
objectBase class to extend when implementing a storage backend.
-
class
limits.storage.MemoryStorage(uri=None, **_)[source]¶ Bases:
limits.storage.Storagerate limit storage using
collections.Counteras an in memory storage for fixed and elastic window strategies, and a simple list to implement moving window strategy.-
acquire_entry(key, limit, expiry, no_add=False)[source]¶ Parameters: - key (str) – rate limit key to acquire an entry in
- limit (int) – amount of entries allowed
- expiry (int) – expiry of the entry
- no_add (bool) – if False an entry is not actually acquired but instead serves as a ‘check’
Returns: True/False
-
get_moving_window(key, limit, expiry)[source]¶ returns the starting point and the number of entries in the moving window
Parameters: - key (str) – rate limit key
- expiry (int) – expiry of entry
-
-
class
limits.storage.RedisStorage(uri, **_)[source]¶ Bases:
limits.storage.RedisInteractor,limits.storage.Storagerate limit storage with redis as backend
-
acquire_entry(key, limit, expiry, no_add=False)[source]¶ Parameters: - key (str) – rate limit key to acquire an entry in
- limit (int) – amount of entries allowed
- expiry (int) – expiry of the entry
- no_add (bool) – if False an entry is not actually acquired but instead serves as a ‘check’
Returns: True/False
-
-
class
limits.storage.RedisSentinelStorage(uri, **options)[source]¶ Bases:
limits.storage.RedisStoragerate limit storage with redis sentinel as backend
-
class
limits.storage.MemcachedStorage(uri, **options)[source]¶ Bases:
limits.storage.Storagerate limit storage with memcached as backend
-
get_client(module, hosts)[source]¶ returns a memcached client. :param module: the memcached module :param hosts: list of memcached hosts :return:
-
incr(key, expiry, elastic_expiry=False)[source]¶ increments the counter for a given rate limit key
Parameters: - key (str) – the key to increment
- expiry (int) – amount in seconds for the key to expire in
- elastic_expiry (bool) – whether to keep extending the rate limit window every hit.
-
storage¶ lazily creates a memcached client instance using a thread local
-
Strategies¶
-
class
limits.strategies.RateLimiter(storage)[source]¶ Bases:
object-
get_window_stats(item, *identifiers)[source]¶ returns the number of requests remaining and reset of this limit.
Parameters: - item – a
RateLimitIteminstance - identifiers – variable list of strings to uniquely identify the limit
Returns: tuple (reset time (int), remaining (int))
- item – a
-
hit(item, *identifiers)[source]¶ creates a hit on the rate limit and returns True if successful.
Parameters: - item – a
RateLimitIteminstance - identifiers – variable list of strings to uniquely identify the limit
Returns: True/False
- item – a
-
test(item, *identifiers)[source]¶ checks the rate limit and returns True if it is not currently exceeded.
Parameters: - item – a
RateLimitIteminstance - identifiers – variable list of strings to uniquely identify the limit
Returns: True/False
- item – a
-
-
class
limits.strategies.FixedWindowRateLimiter(storage)[source]¶ Bases:
limits.strategies.RateLimiterReference: Fixed Window
-
get_window_stats(item, *identifiers)[source]¶ returns the number of requests remaining and reset of this limit.
Parameters: - item – a
RateLimitIteminstance - identifiers – variable list of strings to uniquely identify the limit
Returns: tuple (reset time (int), remaining (int))
- item – a
-
hit(item, *identifiers)[source]¶ creates a hit on the rate limit and returns True if successful.
Parameters: - item – a
RateLimitIteminstance - identifiers – variable list of strings to uniquely identify the limit
Returns: True/False
- item – a
-
test(item, *identifiers)[source]¶ checks the rate limit and returns True if it is not currently exceeded.
Parameters: - item – a
RateLimitIteminstance - identifiers – variable list of strings to uniquely identify the limit
Returns: True/False
- item – a
-
-
class
limits.strategies.FixedWindowElasticExpiryRateLimiter(storage)[source]¶ Bases:
limits.strategies.FixedWindowRateLimiterReference: Fixed Window with Elastic Expiry
-
hit(item, *identifiers)[source]¶ creates a hit on the rate limit and returns True if successful.
Parameters: - item – a
RateLimitIteminstance - identifiers – variable list of strings to uniquely identify the limit
Returns: True/False
- item – a
-
-
class
limits.strategies.MovingWindowRateLimiter(storage)[source]¶ Bases:
limits.strategies.RateLimiterReference: Moving Window
-
get_window_stats(item, *identifiers)[source]¶ returns the number of requests remaining within this limit.
Parameters: - item – a
RateLimitIteminstance - identifiers – variable list of strings to uniquely identify the limit
Returns: tuple (reset time (int), remaining (int))
- item – a
-
hit(item, *identifiers)[source]¶ creates a hit on the rate limit and returns True if successful.
Parameters: - item – a
RateLimitIteminstance - identifiers – variable list of strings to uniquely identify the limit
Returns: True/False
- item – a
-
test(item, *identifiers)[source]¶ checks the rate limit and returns True if it is not currently exceeded.
Parameters: - item – a
RateLimitIteminstance - identifiers – variable list of strings to uniquely identify the limit
Returns: True/False
- item – a
-
Rate Limits¶
-
class
limits.RateLimitItem(amount, multiples=1, namespace='LIMITER')[source]¶ Bases:
objectdefines a Rate limited resource which contains the characteristic namespace, amount and granularity multiples of the rate limiting window.
Parameters: - amount (int) – the rate limit amount
- multiples (int) – multiple of the ‘per’ granularity (e.g. ‘n’ per ‘m’ seconds)
- namespace (string) – category for the specific rate limit
-
class
limits.RateLimitItemPerYear(amount, multiples=1, namespace='LIMITER')[source]¶ Bases:
limits.limits.RateLimitItemper year rate limited resource.
-
class
limits.RateLimitItemPerMonth(amount, multiples=1, namespace='LIMITER')[source]¶ Bases:
limits.limits.RateLimitItemper month rate limited resource.
-
class
limits.RateLimitItemPerDay(amount, multiples=1, namespace='LIMITER')[source]¶ Bases:
limits.limits.RateLimitItemper day rate limited resource.
-
class
limits.RateLimitItemPerHour(amount, multiples=1, namespace='LIMITER')[source]¶ Bases:
limits.limits.RateLimitItemper hour rate limited resource.
-
class
limits.RateLimitItemPerMinute(amount, multiples=1, namespace='LIMITER')[source]¶ Bases:
limits.limits.RateLimitItemper minute rate limited resource.
-
class
limits.RateLimitItemPerSecond(amount, multiples=1, namespace='LIMITER')[source]¶ Bases:
limits.limits.RateLimitItemper second rate limited resource.
-
limits.parse(limit_string)[source]¶ parses a single rate limit in string notation (e.g. ‘1/second’ or ‘1 per second’
Parameters: limit_string (string) – rate limit string using Rate limit string notation Raises: ValueError – if the string notation is invalid. Returns: an instance of RateLimitItem
-
limits.parse_many(limit_string)[source]¶ parses rate limits in string notation containing multiple rate limits (e.g. ‘1/second; 5/minute’)
Parameters: limit_string (string) – rate limit string using Rate limit string notation Raises: ValueError – if the string notation is invalid. Returns: a list of RateLimitIteminstances.