{"id":16646645,"url":"https://github.com/alexdelorenzo/limiter","last_synced_at":"2025-03-16T22:31:52.731Z","repository":{"id":44492185,"uuid":"303243704","full_name":"alexdelorenzo/limiter","owner":"alexdelorenzo","description":"⏲️ Easy rate limiting for Python using a token bucket algorithm, with async and thread-safe decorators and context managers","archived":false,"fork":false,"pushed_at":"2024-02-26T22:11:22.000Z","size":120,"stargazers_count":38,"open_issues_count":4,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-13T08:42:46.157Z","etag":null,"topics":["context-manager","decorators","networking","python","python-asyncio","python3","rate-limiting","token-bucket","traffic-shaping"],"latest_commit_sha":null,"homepage":"https://alexdelorenzo.dev/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexdelorenzo.png","metadata":{"files":{"readme":"README-0.2.0.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"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},"funding":{"github":["alexdelorenzo"]}},"created_at":"2020-10-12T01:04:16.000Z","updated_at":"2024-10-12T08:25:55.000Z","dependencies_parsed_at":"2024-02-18T00:27:52.578Z","dependency_job_id":"fa6d303d-8a28-445f-907b-bca02b851659","html_url":"https://github.com/alexdelorenzo/limiter","commit_stats":{"total_commits":92,"total_committers":4,"mean_commits":23.0,"dds":0.5543478260869565,"last_synced_commit":"5e9bd02a5e93939709c6883416289104a89d2c5b"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdelorenzo%2Flimiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdelorenzo%2Flimiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdelorenzo%2Flimiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdelorenzo%2Flimiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexdelorenzo","download_url":"https://codeload.github.com/alexdelorenzo/limiter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221668784,"owners_count":16860728,"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":["context-manager","decorators","networking","python","python-asyncio","python3","rate-limiting","token-bucket","traffic-shaping"],"created_at":"2024-10-12T08:42:42.788Z","updated_at":"2024-10-27T11:29:39.292Z","avatar_url":"https://github.com/alexdelorenzo.png","language":"Python","funding_links":["https://github.com/sponsors/alexdelorenzo"],"categories":[],"sub_categories":[],"readme":"# ⏲️ Easy rate limiting for Python\n\n`limiter` makes it easy to add [rate limiting](https://en.wikipedia.org/wiki/Rate_limiting) to Python projects, using a [token bucket](https://en.wikipedia.org/wiki/Token_bucket) algorithm. `limiter` can provide Python projects and scripts with:\n  - Rate limiting thread-safe [decorators](https://www.python.org/dev/peps/pep-0318/)\n  - Rate limiting async decorators\n  - Rate limiting thread-safe [context-managers](https://www.python.org/dev/peps/pep-0343/)\n  - Rate limiting [async context-managers](https://www.python.org/dev/peps/pep-0492/#asynchronous-context-managers-and-async-with)\n\nHere are a few benefits of using `limiter`:\n - Easily control burst and average request rates\n - `limiter` is [thread-safe, with no need for a timer thread](https://en.wikipedia.org/wiki/Generic_cell_rate_algorithm)\n - Has a simple API that takes advantage of Python's features, idioms and [type hinting](https://www.python.org/dev/peps/pep-0483/)\n\n# Usage\nYou can define [dynamic](#dynamic-limit) and [static](#static-limit) limiters, and use them across your project.\n\n### Dynamic `limit`\nYou can define a limiter with a set `rate` and `capacity`. Then you can consume a dynamic amount of tokens from different buckets using `limit()`:\n```python3\nfrom limiter import get_limiter, limit\n\n\nREFRESH_RATE: int = 2\nBURST_RATE: int = 3\nMSG_BUCKET: bytes = b'messages'\n\n\nlimiter = get_limiter(rate=REFRESH_RATE, capacity=BURST_RATE)\n\n\n@limit(limiter)\ndef download_page(url: str) -\u003e bytes:\n    ...\n\n\n@limit(limiter, consume=2)\nasync def download_page(url: str) -\u003e bytes:\n    ...\n\n\ndef send_page(page: bytes):\n    with limit(limiter, consume=1.5):\n        ...\n\n\nasync def send_page(page: bytes):\n    async with limit(limiter):\n        ...\n\n\n@limit(limiter, bucket=MSG_BUCKET)\ndef send_email(to: str):\n    ...\n\n\nasync def send_email(to: str):\n    async with limit(limiter, bucket=MSG_BUCKET):\n        ...\n```\n\n### Static `limit`\nYou can define a static `limit` and share it between blocks of code:\n```python\nlimit_downloads = limit(limter, consume=2)\n\n\n@limit_downloads\ndef download_page(url: str) -\u003e bytes:\n    ...\n\n\n@limit_downloads\nasync def download_page(url: str) -\u003e bytes:\n    ...\n\n\ndef download_image(url: str) -\u003e bytes:\n    with limit_downloads:\n        ...\n\n\nasync def download_image(url: str) -\u003e bytes:\n    async with limit_downloads:\n        ...\n```\n\n# Installation\n## Requirements\n - Python 3.7+\n \n## Installing from PyPI\n```bash\npython3 -m pip install limiter\n```\n\n# License\n\nSee [`LICENSE`](/LICENSE). If you'd like to use this project with a different license, please get in touch.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdelorenzo%2Flimiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexdelorenzo%2Flimiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdelorenzo%2Flimiter/lists"}