Skip to content

AYMENJD/py-redis-ratelimit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

py-redis-ratelimit version downloads

A simple asynchronous rate limiter based on redis.

Requirements

Installation

pip install py-redis-ratelimit

Examples

Basic example:

from redis.asyncio import Redis
import ratelimit, asyncio

redis = Redis(decode_responses=True)
limiter = ratelimit.RateLimit(
    redis, prefix="api_rate_limit", rate=10, period=60, retry_after=20
)
print(ratelimit.RateLimit.__doc__)  # print RateLimit class docstring


async def do_something():
    await limiter.acquire(
        identifier="do_something_function"
    )  # a unique identifier for the function. This let's RateLimit know what service/resource you are trying to access.
    ...


async def main():
    for x in range(40):
        try:
            print("Calling do_something() for the {}th time".format(x + 1))
            await do_something()
        except ratelimit.FloodWait as e:
            print("Exception:", e.to_dict())
            break


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

Contributing

Pull requests are always welcome!!

License

MIT License

About

A simple asyncio-based rate limiter for python using Redis.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages