Python Rate Limiter based on Redis.
Project description
This lib offers an abstraction of a Rate Limit algorithm implemented on top of Redis >= 2.6.0.
Example: 10 requests per second
>>> from redis_rate_limit import RateLimit, TooManyRequests
>>> try:
>>> with RateLimit(resource='users_list', client='192.168.0.10', max_requests=10):
>>> return '200 OK'
>>> except TooManyRequests:
>>> return '429 Too Many Requests'
>>>
Example: 600 requests per minute
>>> from redis_rate_limit import RateLimit, TooManyRequests
>>> try:
>>> with RateLimit(resource='users_list', client='192.168.0.10', max_requests=600, expire=60):
>>> return '200 OK'
>>> except TooManyRequests:
>>> return '429 Too Many Requests'
>>>
Example: 100 requests per hour
>>> from redis_rate_limit import RateLimit, TooManyRequests
>>> try:
>>> with RateLimit(resource='users_list', client='192.168.0.10', max_requests=100, expire=3600):
>>> return '200 OK'
>>> except TooManyRequests:
>>> return '429 Too Many Requests'
>>>
Example: you can also setup a factory to use it later
>>> from redis_rate_limit import RateLimiter, TooManyRequests
>>> limiter = RateLimiter(resource='users_list', max_requests=100, expire=3600)
>>> try:
>>> with limiter.limit(client='192.168.0.10'):
>>> return '200 OK'
>>> except TooManyRequests:
>>> return '429 Too Many Requests'
>>>
Example: you can also pass an optional Redis Pool
>>> import redis
>>> from redis_rate_limit import RateLimit, TooManyRequests
>>> redis_pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
>>> try:
>>> with RateLimit(resource='users_list', client='192.168.0.10', max_requests=10, redis_pool=redis_pool):
>>> return '200 OK'
>>> except TooManyRequests:
>>> return '429 Too Many Requests'
>>>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for python-redis-rate-limit-0.0.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9789044478a2161b70893486b0ddc0d1d4dfc9ee38be81b639670408f165a7b5 |
|
MD5 | 474cc521639dc4d50128b99d0619be5d |
|
BLAKE2b-256 | 2e4a013f5587deb46b288252aaa7b9ec2e519386e90e0afc93bc9133875a397e |