{"id":16453086,"url":"https://github.com/alisaifee/limits","last_synced_at":"2025-05-13T20:22:41.583Z","repository":{"id":25517204,"uuid":"28948972","full_name":"alisaifee/limits","owner":"alisaifee","description":"Rate limiting using various strategies and storage backends such as redis, memcached \u0026 mongodb","archived":false,"fork":false,"pushed_at":"2025-04-23T20:03:15.000Z","size":1614,"stargazers_count":496,"open_issues_count":0,"forks_count":66,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-28T12:04:00.862Z","etag":null,"topics":["async","memcached","mongodb","python","rate-limiting","redis","valkey"],"latest_commit_sha":null,"homepage":"https://limits.readthedocs.org","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alisaifee.png","metadata":{"files":{"readme":"README.rst","changelog":"HISTORY.rst","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"alisaifee","open_collective":"limits"}},"created_at":"2015-01-08T04:34:40.000Z","updated_at":"2025-04-26T09:28:01.000Z","dependencies_parsed_at":"2023-09-22T22:29:05.235Z","dependency_job_id":"1ae20937-9ddd-4cc3-b76a-7f45b0845441","html_url":"https://github.com/alisaifee/limits","commit_stats":{"total_commits":892,"total_committers":26,"mean_commits":34.30769230769231,"dds":0.094170403587444,"last_synced_commit":"ada96bb4afc9729b4aac2552209a78428a27c313"},"previous_names":[],"tags_count":89,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alisaifee%2Flimits","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alisaifee%2Flimits/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alisaifee%2Flimits/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alisaifee%2Flimits/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alisaifee","download_url":"https://codeload.github.com/alisaifee/limits/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251311332,"owners_count":21569009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","memcached","mongodb","python","rate-limiting","redis","valkey"],"created_at":"2024-10-11T10:14:39.546Z","updated_at":"2025-04-28T12:04:11.883Z","avatar_url":"https://github.com/alisaifee.png","language":"Python","readme":".. |ci| image:: https://github.com/alisaifee/limits/actions/workflows/main.yml/badge.svg?branch=master\n    :target: https://github.com/alisaifee/limits/actions?query=branch%3Amaster+workflow%3ACI\n.. |codecov| image:: https://codecov.io/gh/alisaifee/limits/branch/master/graph/badge.svg\n   :target: https://codecov.io/gh/alisaifee/limits\n.. |pypi| image:: https://img.shields.io/pypi/v/limits.svg?style=flat-square\n    :target: https://pypi.python.org/pypi/limits\n.. |pypi-versions| image:: https://img.shields.io/pypi/pyversions/limits?style=flat-square\n    :target: https://pypi.python.org/pypi/limits\n.. |license| image:: https://img.shields.io/pypi/l/limits.svg?style=flat-square\n    :target: https://pypi.python.org/pypi/limits\n.. |docs| image:: https://readthedocs.org/projects/limits/badge/?version=latest\n   :target: https://limits.readthedocs.org\n\n######\nlimits\n######\n|docs| |ci| |codecov| |pypi| |pypi-versions| |license|\n\n\n**limits** is a python library for rate limiting via multiple strategies\nwith commonly used storage backends (Redis, Memcached \u0026 MongoDB).\n\nThe library provides identical APIs for use in sync and\n`async \u003chttps://limits.readthedocs.io/en/stable/async.html\u003e`_ codebases.\n\n\nSupported Strategies\n====================\n\nAll strategies support the follow methods:\n\n- `hit \u003chttps://limits.readthedocs.io/en/stable/api.html#limits.strategies.RateLimiter.hit\u003e`_: consume a request.\n- `test \u003chttps://limits.readthedocs.io/en/stable/api.html#limits.strategies.RateLimiter.test\u003e`_: check if a request is allowed.\n- `get_window_stats \u003chttps://limits.readthedocs.io/en/stable/api.html#limits.strategies.RateLimiter.get_window_stats\u003e`_: retrieve remaining quota and reset time.\n\nFixed Window\n------------\n`Fixed Window \u003chttps://limits.readthedocs.io/en/latest/strategies.html#fixed-window\u003e`_\n\nThis strategy is the most memory‑efficient because it uses a single counter per resource and\nrate limit. When the first request arrives, a window is started for a fixed duration\n(e.g., for a rate limit of 10 requests per minute the window expires in 60 seconds from the first request).\nAll requests in that window increment the counter and when the window expires, the counter resets.\n\nBurst traffic that bypasses the rate limit may occur at window boundaries.\n\nFor example, with a rate limit of 10 requests per minute:\n\n- At **00:00:45**, the first request arrives, starting a window from **00:00:45** to **00:01:45**.\n- All requests between **00:00:45** and **00:01:45** count toward the limit.\n- If 10 requests occur at any time in that window, any further request before **00:01:45** is rejected.\n- At **00:01:45**, the counter resets and a new window starts which would allow 10 requests\n  until **00:02:45**.\n\nMoving Window\n-------------\n`Moving Window \u003chttps://limits.readthedocs.io/en/latest/strategies.html#moving-window\u003e`_\n\nThis strategy adds each request’s timestamp to a log if the ``nth`` oldest entry (where ``n``\nis the limit) is either not present or is older than the duration of the window (for example with a rate limit of\n``10 requests per minute`` if there are either less than 10 entries or the 10th oldest entry is at least\n60 seconds old). Upon adding a new entry to the log \"expired\" entries are truncated.\n\nFor example, with a rate limit of 10 requests per minute:\n\n- At **00:00:10**, a client sends 1 requests which are allowed.\n- At **00:00:20**, a client sends 2 requests which are allowed.\n- At **00:00:30**, the client sends 4 requests which are allowed.\n- At **00:00:50**, the client sends 3 requests which are allowed (total = 10).\n- At **00:01:11**, the client sends 1 request. The strategy checks the timestamp of the\n  10th oldest entry (**00:00:10**) which is now 61 seconds old and thus expired. The request\n  is allowed.\n- At **00:01:12**, the client sends 1 request. The 10th oldest entry's timestamp is **00:00:20**\n  which is only 52 seconds old. The request is rejected.\n\nSliding Window Counter\n------------------------\n`Sliding Window Counter \u003chttps://limits.readthedocs.io/en/latest/strategies.html#sliding-window-counter\u003e`_\n\nThis strategy approximates the moving window while using less memory by maintaining\ntwo counters:\n\n- **Current bucket:** counts requests in the ongoing period.\n- **Previous bucket:** counts requests in the immediately preceding period.\n\nWhen a request arrives, the effective request count is calculated as::\n\n    weighted_count = current_count + floor(previous_count * weight)\n\nThe weight is based on how much time has elapsed in the current bucket::\n\n    weight = (bucket_duration - elapsed_time) / bucket_duration\n\nIf ``weighted_count`` is below the limit, the request is allowed.\n\nFor example, with a rate limit of 10 requests per minute:\n\nAssume:\n\n- The current bucket (spanning **00:01:00** to **00:02:00**) has 8 hits.\n- The previous bucket (spanning **00:00:00** to **00:01:00**) has 4 hits.\n\nScenario 1:\n\n- A new request arrives at **00:01:30**, 30 seconds into the current bucket.\n- ``weight = (60 - 30) / 60 = 0.5``.\n- ``weighted_count = floor(8 + (4 * 0.5)) = floor(8 + 2) = 10``.\n- Since the weighted count equals the limit, the request is rejected.\n\nScenario 2:\n\n- A new request arrives at **00:01:40**, 40 seconds into the current bucket.\n- ``weight = (60 - 40) / 60 ≈ 0.33``.\n- ``weighted_count = floor(8 + (4 * 0.33)) = floor(8 + 1.32) = 9``.\n- Since the weighted count is below the limit, the request is allowed.\n\n\nStorage backends\n================\n\n- `Redis \u003chttps://limits.readthedocs.io/en/latest/storage.html#redis-storage\u003e`_\n- `Memcached \u003chttps://limits.readthedocs.io/en/latest/storage.html#memcached-storage\u003e`_\n- `MongoDB \u003chttps://limits.readthedocs.io/en/latest/storage.html#mongodb-storage\u003e`_\n- `In-Memory \u003chttps://limits.readthedocs.io/en/latest/storage.html#in-memory-storage\u003e`_\n\nDive right in\n=============\n\nInitialize the storage backend\n\n.. code-block:: python\n\n   from limits import storage\n   backend = storage.MemoryStorage()\n   # or memcached\n   backend = storage.MemcachedStorage(\"memcached://localhost:11211\")\n   # or redis\n   backend = storage.RedisStorage(\"redis://localhost:6379\")\n   # or mongodb\n   backend = storage.MongoDbStorage(\"mongodb://localhost:27017\")\n   # or use the factory\n   storage_uri = \"memcached://localhost:11211\"\n   backend = storage.storage_from_string(storage_uri)\n\nInitialize a rate limiter with a strategy\n\n.. code-block:: python\n\n   from limits import strategies\n   strategy = strategies.MovingWindowRateLimiter(backend)\n   # or fixed window\n   strategy = strategies.FixedWindowRateLimiter(backend)\n   # or sliding window\n   strategy = strategies.SlidingWindowCounterRateLimiter(backend)\n\n\nInitialize a rate limit\n\n.. code-block:: python\n\n    from limits import parse\n    one_per_minute = parse(\"1/minute\")\n\nInitialize a rate limit explicitly\n\n.. code-block:: python\n\n    from limits import RateLimitItemPerSecond\n    one_per_second = RateLimitItemPerSecond(1, 1)\n\nTest the limits\n\n.. code-block:: python\n\n    import time\n    assert True == strategy.hit(one_per_minute, \"test_namespace\", \"foo\")\n    assert False == strategy.hit(one_per_minute, \"test_namespace\", \"foo\")\n    assert True == strategy.hit(one_per_minute, \"test_namespace\", \"bar\")\n\n    assert True == strategy.hit(one_per_second, \"test_namespace\", \"foo\")\n    assert False == strategy.hit(one_per_second, \"test_namespace\", \"foo\")\n    time.sleep(1)\n    assert True == strategy.hit(one_per_second, \"test_namespace\", \"foo\")\n\nCheck specific limits without hitting them\n\n.. code-block:: python\n\n    assert True == strategy.hit(one_per_second, \"test_namespace\", \"foo\")\n    while not strategy.test(one_per_second, \"test_namespace\", \"foo\"):\n        time.sleep(0.01)\n    assert True == strategy.hit(one_per_second, \"test_namespace\", \"foo\")\n\nQuery available capacity and reset time for a limit\n\n.. code-block:: python\n\n   assert True == strategy.hit(one_per_minute, \"test_namespace\", \"foo\")\n   window = strategy.get_window_stats(one_per_minute, \"test_namespace\", \"foo\")\n   assert window.remaining == 0\n   assert False == strategy.hit(one_per_minute, \"test_namespace\", \"foo\")\n   time.sleep(window.reset_time - time.time())\n   assert True == strategy.hit(one_per_minute, \"test_namespace\", \"foo\")\n\n\nLinks\n=====\n\n* `Documentation \u003chttp://limits.readthedocs.org/en/latest\u003e`_\n* `Benchmarks \u003chttp://limits.readthedocs.org/en/latest/performance.html\u003e`_\n* `Changelog \u003chttp://limits.readthedocs.org/en/stable/changelog.html\u003e`_\n\n","funding_links":["https://github.com/sponsors/alisaifee","https://opencollective.com/limits"],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falisaifee%2Flimits","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falisaifee%2Flimits","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falisaifee%2Flimits/lists"}