aioredis-rate-limiter


Nameaioredis-rate-limiter JSON
Version 0.1.0 PyPI version JSON
download
home_page
SummaryRate limiter implements on Redis
upload_time2023-03-24 03:37:15
maintainer
docs_urlNone
authorDFilyushin
requires_python>=3.10,<4.0
licenseMIT
keywords aioredis redis rate-limiting distributed asyncio
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # aioredis-rate-limiter
***

Rate limiter implements on Redis

Implementation of distributed rate limiter with aioredis, an asyncio based redis client.

Simple rate limiter based on Redis DB. 

The key features are:

* Concurrency work
* Rate by limit requests
* Rate by time for requests

# Usage

We want to limit requests from 5 pods to limited resource. Limits for resource: no more 10 request per 20 seconds.

```python
locker = AioRedisRateLimiter(redis, rate_limit=10, rate_key_ttl=20)
```

```python

import asyncio
import os
from aioredis.client import Redis
from aioredis_rate_limiter import AioRedisRateLimiter

class Executor:
    def __init__(self, name: str, locker: AioRedisRateLimiter, task_count: int = 10):
        self._locker = locker
        self._task_count = task_count
        self._name = name

    async def process(self):
        for i in range(self._task_count):
            while True:
                is_ok = await self._locker.acquire()
                if is_ok:
                    print(f'Executor {self._name} by {i+1}')
                    break
                else:
                    await asyncio.sleep(1)


async def main():
    host = os.getenv('REDIS_HOST')
    db = os.getenv('REDIS_DB')
    
    redis = Redis.from_url(host, db=db, encoding="utf-8", decode_responses=True)

    locker = AioRedisRateLimiter(redis, rate_limit=10, rate_key_ttl=15)

    w1 = Executor('first', locker, 10)
    w2 = Executor('helper', locker, 8)
    w3 = Executor('lazzy', locker, 5)

    tasks = [w1.process(), w2.process(), w3.process()]

    await asyncio.gather(*tasks)

    
if __name__ == '__main__':
    asyncio.run(main())

```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "aioredis-rate-limiter",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "aioredis,redis,rate-limiting,distributed,asyncio",
    "author": "DFilyushin",
    "author_email": "Dmitriy.Filyushin@yandex.ru",
    "download_url": "https://files.pythonhosted.org/packages/9a/29/b513638b102f388d4b3a8d2bc45792fb376481170addb7b37ad85fa8fc41/aioredis_rate_limiter-0.1.0.tar.gz",
    "platform": null,
    "description": "# aioredis-rate-limiter\n***\n\nRate limiter implements on Redis\n\nImplementation of distributed rate limiter with aioredis, an asyncio based redis client.\n\nSimple rate limiter based on Redis DB. \n\nThe key features are:\n\n* Concurrency work\n* Rate by limit requests\n* Rate by time for requests\n\n# Usage\n\nWe want to limit requests from 5 pods to limited resource. Limits for resource: no more 10 request per 20 seconds.\n\n```python\nlocker = AioRedisRateLimiter(redis, rate_limit=10, rate_key_ttl=20)\n```\n\n```python\n\nimport asyncio\nimport os\nfrom aioredis.client import Redis\nfrom aioredis_rate_limiter import AioRedisRateLimiter\n\nclass Executor:\n    def __init__(self, name: str, locker: AioRedisRateLimiter, task_count: int = 10):\n        self._locker = locker\n        self._task_count = task_count\n        self._name = name\n\n    async def process(self):\n        for i in range(self._task_count):\n            while True:\n                is_ok = await self._locker.acquire()\n                if is_ok:\n                    print(f'Executor {self._name} by {i+1}')\n                    break\n                else:\n                    await asyncio.sleep(1)\n\n\nasync def main():\n    host = os.getenv('REDIS_HOST')\n    db = os.getenv('REDIS_DB')\n    \n    redis = Redis.from_url(host, db=db, encoding=\"utf-8\", decode_responses=True)\n\n    locker = AioRedisRateLimiter(redis, rate_limit=10, rate_key_ttl=15)\n\n    w1 = Executor('first', locker, 10)\n    w2 = Executor('helper', locker, 8)\n    w3 = Executor('lazzy', locker, 5)\n\n    tasks = [w1.process(), w2.process(), w3.process()]\n\n    await asyncio.gather(*tasks)\n\n    \nif __name__ == '__main__':\n    asyncio.run(main())\n\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Rate limiter implements on Redis",
    "version": "0.1.0",
    "split_keywords": [
        "aioredis",
        "redis",
        "rate-limiting",
        "distributed",
        "asyncio"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4b1d23e9035ce1f59c3671c6bd4e63bd2d3b70fab0ebb7f5d1ba4be927843235",
                "md5": "c586947f568845f6b9bc6eb701adb626",
                "sha256": "983d03e87bd8a6e2b2fa61008658e4b60881440400bfa915fa29a905e5bb4f6f"
            },
            "downloads": -1,
            "filename": "aioredis_rate_limiter-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c586947f568845f6b9bc6eb701adb626",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 3570,
            "upload_time": "2023-03-24T03:37:13",
            "upload_time_iso_8601": "2023-03-24T03:37:13.745325Z",
            "url": "https://files.pythonhosted.org/packages/4b/1d/23e9035ce1f59c3671c6bd4e63bd2d3b70fab0ebb7f5d1ba4be927843235/aioredis_rate_limiter-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9a29b513638b102f388d4b3a8d2bc45792fb376481170addb7b37ad85fa8fc41",
                "md5": "a9cdf589cf3799393b4f5452c1bf3bb7",
                "sha256": "7158f50a4fa53a48b2d739df3331b6b13fb0c500fe6106219f36859ed5c9f2ea"
            },
            "downloads": -1,
            "filename": "aioredis_rate_limiter-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "a9cdf589cf3799393b4f5452c1bf3bb7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 2751,
            "upload_time": "2023-03-24T03:37:15",
            "upload_time_iso_8601": "2023-03-24T03:37:15.012029Z",
            "url": "https://files.pythonhosted.org/packages/9a/29/b513638b102f388d4b3a8d2bc45792fb376481170addb7b37ad85fa8fc41/aioredis_rate_limiter-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-24 03:37:15",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "aioredis-rate-limiter"
}
        
Elapsed time: 0.82646s