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'
>>>
You can also setup factory to use later. Example:
>>> 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'
>>>