redis-limiters


Nameredis-limiters JSON
Version 0.6.0 PyPI version JSON
download
home_pageNone
SummaryPython rate limiters backed by Redis
upload_time2025-08-18 16:53:30
maintainerNone
docs_urlNone
authorEmil Aliyev, Sondre Lillebø Gundersen
requires_python>=3.11
licenseNone
keywords redis async sync rate limiting limiters
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Python Redis Limiters

A library which regulates traffic, with respect to concurrency or time.
It implements sync and async context managers for a [semaphore](#semaphore)- and a [token bucket](#token-bucket)-implementation.

The rate limiters are distributed, using Redis, and leverages Lua scripts to
improve performance and simplify the code. Lua scripts
run on Redis, and make each implementation fully atomic, while
also reducing the number of round-trips required.

Use is supported for standalone redis instances, and clusters.
We currently only support Python 3.11, but can add support for older versions if needed.

## NOTE:
This project was initially forked from [redis-rate-limiters](https://github.com/otovo/redis-rate-limiters) and was mainly created by Sondre Lillebø Gundersen [link](https://github.com/sondrelg).

The old project is no longer being worked on and only supported PydanticV1. I plan to add more functionality as well as maintain this fork in the future. It will be published under py-redis-limiters.

Currently I:
 - migrated to PydanticV2
 - migrated from poetry to uv
 - migrated from just to mise en place
 - changed the pre-commit & build process a bit (e.g. remove black/isort in favor of ruff)
 - tidied up a few types as well as add types to tests
 - added a few more tests (I plan to add more)
 - added default values to the rate limits.

## Installation

```
pip install py-redis-limiters
```

## Usage

### Semaphore

The semaphore classes are useful when you have concurrency restrictions;
e.g., say you're allowed 5 active requests at the time for a given API token.

Beware that the client will block until the Semaphore is acquired,
or the `max_sleep` limit is exceeded. If the `max_sleep` limit is exceeded, a `MaxSleepExceededError` is raised. Setting `max_sleep` to 0.0 will sleep "endlessly" - this is also the default value.

Here's how you might use the async version:

```python
import asyncio

from httpx import AsyncClient
from redis.asyncio import Redis

from limiters import AsyncSemaphore

# Every property besides name has a default like below
limiter = AsyncSemaphore(
    name="foo",    # name of the resource you are limiting traffic for
    capacity=5,    # allow 5 concurrent requests
    max_sleep=30,  # raise an error if it takes longer than 30 seconds to acquire the semaphore
    expiry=30,      # set expiry on the semaphore keys in Redis to prevent deadlocks
    connection=Redis.from_url("redis://localhost:6379"),
)

async def get_foo():
    async with AsyncClient() as client:
        async with limiter:
            client.get(...)


async def main():
    await asyncio.gather(
        get_foo() for i in range(100)
    )
```

and here is how you might use the sync version:

```python
import requests
from redis import Redis

from limiters import SyncSemaphore


limiter = SyncSemaphore(
    name="foo",
    capacity=5,
    max_sleep=30,
    expiry=30,
    connection=Redis.from_url("redis://localhost:6379"),
)

def main():
    with limiter:
        requests.get(...)
```

### Token bucket

The `TocketBucket` classes are useful if you're working with time-based
rate limits. Say, you are allowed 100 requests per minute, for a given API token.

If the `max_sleep` limit is exceeded, a `MaxSleepExceededError` is raised. Setting `max_sleep` to 0.0 will sleep "endlessly" - this is also the default value.

Here's how you might use the async version:

```python
import asyncio

from httpx import AsyncClient
from redis.asyncio import Redis

from limiters import AsyncTokenBucket

# Every property besides name has a default like below
limiter = AsyncTokenBucket(
    name="foo",          # name of the resource you are limiting traffic for
    capacity=5,          # hold up to 5 tokens
    refill_frequency=1,  # add tokens every second
    refill_amount=1,     # add 1 token when refilling
    max_sleep=0,         # raise an error if there are no free tokens for X seconds, 0 never expires
    connection=Redis.from_url("redis://localhost:6379"),
)

async def get_foo():
    async with AsyncClient() as client:
        async with limiter:
            client.get(...)

async def main():
    await asyncio.gather(
        get_foo() for i in range(100)
    )
```

and here is how you might use the sync version:

```python
import requests
from redis import Redis

from limiters import SyncTokenBucket


limiter = SyncTokenBucket(
    name="foo",
    capacity=5,
    refill_frequency=1,
    refill_amount=1,
    max_sleep=0,
    connection=Redis.from_url("redis://localhost:6379"),
)

def main():
    with limiter:
        requests.get(...)
```

### Using them as a decorator

We don't ship decorators in the package, but if you would
like to limit the rate at which a whole function is run,
you can create your own, like this:

```python
from limiters import AsyncSemaphore


# Define a decorator function
def limit(name, capacity):
  def middle(f):
    async def inner(*args, **kwargs):
      async with AsyncSemaphore(name=name, capacity=capacity):
        return await f(*args, **kwargs)
    return inner
  return middle


# Then pass the relevant limiter arguments like this
@limit(name="foo", capacity=5)
def fetch_foo(id: UUID) -> Foo:
```

## Contributing

Contributions are very welcome. Here's how to get started:

- Clone the repo
- Install [uv](https://docs.astral.sh/uv/getting-started/installation/)
- Run `pre-commit install` to set up pre-commit
- Install [just](https://just.systems/man/en/) and run `just setup`
  If you prefer not to install just, just take a look at the justfile and
  run the commands yourself.
- Make your code changes, with tests
- Commit your changes and open a PR

## Publishing a new version

To publish a new version:

- Update the package version in the `pyproject.toml`
- Open [Github releases](https://github.com/Feuerstein-Org/py-redis-limiters/releases)
- Press "Draft a new release"
- Set a tag matching the new version (for example, `v0.1.0`)
- Set the title matching the tag
- Add some release notes, explaining what has changed
- Publish

Once the release is published, our [publish workflow](https://github.com/Feuerstein-Org/py-redis-limiters/blob/main/.github/workflows/publish.yaml) should be triggered
to push the new version to PyPI.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "redis-limiters",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "redis, async, sync, rate, limiting, limiters",
    "author": "Emil Aliyev, Sondre Lilleb\u00f8 Gundersen",
    "author_email": "Emil Aliyev <ea01052002@gmail.com>, Sondre Lilleb\u00f8 Gundersen <sondrelg@live.no>",
    "download_url": "https://files.pythonhosted.org/packages/b7/49/752339a32690c402d23a8009aa01443fa04cd71e522602c1d323b62ab286/redis_limiters-0.6.0.tar.gz",
    "platform": null,
    "description": "# Python Redis Limiters\n\nA library which regulates traffic, with respect to concurrency or time.\nIt implements sync and async context managers for a [semaphore](#semaphore)- and a [token bucket](#token-bucket)-implementation.\n\nThe rate limiters are distributed, using Redis, and leverages Lua scripts to\nimprove performance and simplify the code. Lua scripts\nrun on Redis, and make each implementation fully atomic, while\nalso reducing the number of round-trips required.\n\nUse is supported for standalone redis instances, and clusters.\nWe currently only support Python 3.11, but can add support for older versions if needed.\n\n## NOTE:\nThis project was initially forked from [redis-rate-limiters](https://github.com/otovo/redis-rate-limiters) and was mainly created by Sondre Lilleb\u00f8 Gundersen [link](https://github.com/sondrelg).\n\nThe old project is no longer being worked on and only supported PydanticV1. I plan to add more functionality as well as maintain this fork in the future. It will be published under py-redis-limiters.\n\nCurrently I:\n - migrated to PydanticV2\n - migrated from poetry to uv\n - migrated from just to mise en place\n - changed the pre-commit & build process a bit (e.g. remove black/isort in favor of ruff)\n - tidied up a few types as well as add types to tests\n - added a few more tests (I plan to add more)\n - added default values to the rate limits.\n\n## Installation\n\n```\npip install py-redis-limiters\n```\n\n## Usage\n\n### Semaphore\n\nThe semaphore classes are useful when you have concurrency restrictions;\ne.g., say you're allowed 5 active requests at the time for a given API token.\n\nBeware that the client will block until the Semaphore is acquired,\nor the `max_sleep` limit is exceeded. If the `max_sleep` limit is exceeded, a `MaxSleepExceededError` is raised. Setting `max_sleep` to 0.0 will sleep \"endlessly\" - this is also the default value.\n\nHere's how you might use the async version:\n\n```python\nimport asyncio\n\nfrom httpx import AsyncClient\nfrom redis.asyncio import Redis\n\nfrom limiters import AsyncSemaphore\n\n# Every property besides name has a default like below\nlimiter = AsyncSemaphore(\n    name=\"foo\",    # name of the resource you are limiting traffic for\n    capacity=5,    # allow 5 concurrent requests\n    max_sleep=30,  # raise an error if it takes longer than 30 seconds to acquire the semaphore\n    expiry=30,      # set expiry on the semaphore keys in Redis to prevent deadlocks\n    connection=Redis.from_url(\"redis://localhost:6379\"),\n)\n\nasync def get_foo():\n    async with AsyncClient() as client:\n        async with limiter:\n            client.get(...)\n\n\nasync def main():\n    await asyncio.gather(\n        get_foo() for i in range(100)\n    )\n```\n\nand here is how you might use the sync version:\n\n```python\nimport requests\nfrom redis import Redis\n\nfrom limiters import SyncSemaphore\n\n\nlimiter = SyncSemaphore(\n    name=\"foo\",\n    capacity=5,\n    max_sleep=30,\n    expiry=30,\n    connection=Redis.from_url(\"redis://localhost:6379\"),\n)\n\ndef main():\n    with limiter:\n        requests.get(...)\n```\n\n### Token bucket\n\nThe `TocketBucket` classes are useful if you're working with time-based\nrate limits. Say, you are allowed 100 requests per minute, for a given API token.\n\nIf the `max_sleep` limit is exceeded, a `MaxSleepExceededError` is raised. Setting `max_sleep` to 0.0 will sleep \"endlessly\" - this is also the default value.\n\nHere's how you might use the async version:\n\n```python\nimport asyncio\n\nfrom httpx import AsyncClient\nfrom redis.asyncio import Redis\n\nfrom limiters import AsyncTokenBucket\n\n# Every property besides name has a default like below\nlimiter = AsyncTokenBucket(\n    name=\"foo\",          # name of the resource you are limiting traffic for\n    capacity=5,          # hold up to 5 tokens\n    refill_frequency=1,  # add tokens every second\n    refill_amount=1,     # add 1 token when refilling\n    max_sleep=0,         # raise an error if there are no free tokens for X seconds, 0 never expires\n    connection=Redis.from_url(\"redis://localhost:6379\"),\n)\n\nasync def get_foo():\n    async with AsyncClient() as client:\n        async with limiter:\n            client.get(...)\n\nasync def main():\n    await asyncio.gather(\n        get_foo() for i in range(100)\n    )\n```\n\nand here is how you might use the sync version:\n\n```python\nimport requests\nfrom redis import Redis\n\nfrom limiters import SyncTokenBucket\n\n\nlimiter = SyncTokenBucket(\n    name=\"foo\",\n    capacity=5,\n    refill_frequency=1,\n    refill_amount=1,\n    max_sleep=0,\n    connection=Redis.from_url(\"redis://localhost:6379\"),\n)\n\ndef main():\n    with limiter:\n        requests.get(...)\n```\n\n### Using them as a decorator\n\nWe don't ship decorators in the package, but if you would\nlike to limit the rate at which a whole function is run,\nyou can create your own, like this:\n\n```python\nfrom limiters import AsyncSemaphore\n\n\n# Define a decorator function\ndef limit(name, capacity):\n  def middle(f):\n    async def inner(*args, **kwargs):\n      async with AsyncSemaphore(name=name, capacity=capacity):\n        return await f(*args, **kwargs)\n    return inner\n  return middle\n\n\n# Then pass the relevant limiter arguments like this\n@limit(name=\"foo\", capacity=5)\ndef fetch_foo(id: UUID) -> Foo:\n```\n\n## Contributing\n\nContributions are very welcome. Here's how to get started:\n\n- Clone the repo\n- Install [uv](https://docs.astral.sh/uv/getting-started/installation/)\n- Run `pre-commit install` to set up pre-commit\n- Install [just](https://just.systems/man/en/) and run `just setup`\n  If you prefer not to install just, just take a look at the justfile and\n  run the commands yourself.\n- Make your code changes, with tests\n- Commit your changes and open a PR\n\n## Publishing a new version\n\nTo publish a new version:\n\n- Update the package version in the `pyproject.toml`\n- Open [Github releases](https://github.com/Feuerstein-Org/py-redis-limiters/releases)\n- Press \"Draft a new release\"\n- Set a tag matching the new version (for example, `v0.1.0`)\n- Set the title matching the tag\n- Add some release notes, explaining what has changed\n- Publish\n\nOnce the release is published, our [publish workflow](https://github.com/Feuerstein-Org/py-redis-limiters/blob/main/.github/workflows/publish.yaml) should be triggered\nto push the new version to PyPI.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python rate limiters backed by Redis",
    "version": "0.6.0",
    "project_urls": {
        "Homepage": "https://github.com/Feuerstein-Org/redis-limiters",
        "Repository": "https://github.com/Feuerstein-Org/redis-limiters"
    },
    "split_keywords": [
        "redis",
        " async",
        " sync",
        " rate",
        " limiting",
        " limiters"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "35d7eef49cbcb833dab2ade4d45359fc899e157ed513fa4de17b9d222c62659a",
                "md5": "7738de97854274a488512b0aa0cbb2ab",
                "sha256": "70db3f303b1eec63d745c1961bafc2683f9728dcf28cc46efb224c23d3f327ea"
            },
            "downloads": -1,
            "filename": "redis_limiters-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7738de97854274a488512b0aa0cbb2ab",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 10629,
            "upload_time": "2025-08-18T16:53:29",
            "upload_time_iso_8601": "2025-08-18T16:53:29.220246Z",
            "url": "https://files.pythonhosted.org/packages/35/d7/eef49cbcb833dab2ade4d45359fc899e157ed513fa4de17b9d222c62659a/redis_limiters-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b749752339a32690c402d23a8009aa01443fa04cd71e522602c1d323b62ab286",
                "md5": "436d44bf228f3d55bfa94388bccd63ba",
                "sha256": "e0ea94668106a94736903fd10ef661337ad70434f04f8e5c95f665124d5ecec3"
            },
            "downloads": -1,
            "filename": "redis_limiters-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "436d44bf228f3d55bfa94388bccd63ba",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 8380,
            "upload_time": "2025-08-18T16:53:30",
            "upload_time_iso_8601": "2025-08-18T16:53:30.261822Z",
            "url": "https://files.pythonhosted.org/packages/b7/49/752339a32690c402d23a8009aa01443fa04cd71e522602c1d323b62ab286/redis_limiters-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-18 16:53:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Feuerstein-Org",
    "github_project": "redis-limiters",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "redis-limiters"
}
        
Elapsed time: 2.02266s