Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

docs ci codecov pypi pypi-versions license

limits is a python library to perform rate limiting with commonly used storage backends (Redis, Memcached & MongoDB).

Supported Strategies

Fixed Window

This strategy resets at a fixed interval (start of minute, hour, day etc). For example, given a rate limit of 10/minute the strategy will:

  • Allow 10 requests between 00:01:00 and 00:02:00

  • Allow 10 requests at 00:00:59 and 10 more requests at 00:01:00

Fixed Window (Elastic)

Identical to Fixed window, except every breach of rate limit results in an extension to the time out. For example a rate limit of 1/minute hit twice within a minute will result in a lock-out for two minutes.

Moving Window

Sliding window strategy enforces a rate limit of N/(m time units) on the last m time units at the second granularity.

For example, with a rate limit of 10/minute:

  • Allow 9 requests that arrive at 00:00:59

  • Allow another request that arrives at 00:01:00

  • Reject the request that arrives at 00:01:01

Storage backends

Experimental

Dive right in

Initialize the storage backend

from limits import storage
memory_storage = storage.MemoryStorage()
# or memcached
memcached_storage = storage.MemcachedStorage("memcached://localhost:11211")
# or redis
redis_storage = storage.RedisStorage("redis://localhost:6379")
# or leave it to fate
some_storage = storage.storage.from_string(fate)

Initialize a rate limiter with the Moving Window Strategy

from limits import strategies
moving_window = strategies.MovingWindowRateLimiter(memory_storage)

Initialize a rate limit

from limits import parse
one_per_minute = parse("1/minute")

Initialize a rate limit explicitly

from limits import RateLimitItemPerSecond
one_per_second = RateLimitItemPerSecond(1, 1)

Test the limits

assert True == moving_window.hit(one_per_minute, "test_namespace", "foo")
assert False == moving_window.hit(one_per_minute, "test_namespace", "foo")
assert True == moving_window.hit(one_per_minute, "test_namespace", "bar")

assert True == moving_window.hit(one_per_second, "test_namespace", "foo")
assert False == moving_window.hit(one_per_second, "test_namespace", "foo")
time.sleep(1)
assert True == moving_window.hit(one_per_second, "test_namespace", "foo")

Check specific limits without hitting them

assert True == moving_window.hit(one_per_second, "test_namespace", "foo")
while not moving_window.test(one_per_second, "test_namespace", "foo"):
    time.sleep(0.01)
assert True == moving_window.hit(one_per_second, "test_namespace", "foo")

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

limits-2.1.0.b4.tar.gz (58.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

limits-2.1.0b4-py3-none-any.whl (35.6 kB view details)

Uploaded Python 3

File details

Details for the file limits-2.1.0.b4.tar.gz.

File metadata

  • Download URL: limits-2.1.0.b4.tar.gz
  • Upload date:
  • Size: 58.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for limits-2.1.0.b4.tar.gz
Algorithm Hash digest
SHA256 1bba0349da72d1d19257c13a4dc6fb36fcf320bd186a7c6228d3518d83ddf907
MD5 d10cfb8713c218c1c168784300a6341b
BLAKE2b-256 05504dd36eb5452d3d2c7f6c558bbd553a38d68b5d85137acf9511c6e62269e1

See more details on using hashes here.

File details

Details for the file limits-2.1.0b4-py3-none-any.whl.

File metadata

  • Download URL: limits-2.1.0b4-py3-none-any.whl
  • Upload date:
  • Size: 35.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for limits-2.1.0b4-py3-none-any.whl
Algorithm Hash digest
SHA256 8b376baf6efe1b7ac171705d997a5c3da3f7017d4f3b8463ef4c0322fe16e056
MD5 5ae8a2abebce227198c60488f944d86d
BLAKE2b-256 fdf00605f144be61c6132d4c5f9320611d6b0774b3052261785b67d664f2db27

See more details on using hashes here.

Release history Release notifications | RSS feed

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page