Skip to main content

Python Rate Limiter based on Redis.

Project description

https://github.com/EvoluxBR/python-redis-rate-limit/actions/workflows/python-package.yml/badge.svg https://img.shields.io/pypi/v/python-redis-rate-limit.svg https://img.shields.io/pypi/dm/python-redis-rate-limit.svg

This lib offers an abstraction of a Rate Limit algorithm implemented on top of Redis >= 2.6.0.

Supported Python Versions: 2.7, 3.6+

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: using as a decorator

from redis_rate_limit import RateLimit, TooManyRequests

@RateLimit(resource='users_list', client='192.168.0.10', max_requests=10)
def list_users():
  return '200 OK'

try:
  return list_users()
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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-redis-rate-limit-0.0.10.tar.gz (4.7 kB view details)

Uploaded Source

File details

Details for the file python-redis-rate-limit-0.0.10.tar.gz.

File metadata

File hashes

Hashes for python-redis-rate-limit-0.0.10.tar.gz
Algorithm Hash digest
SHA256 f73b7df1b2989b1dce2bcb5870d19007648b7a445b6b010e4f5ff986d38c3694
MD5 edd7fde5915236fb297625cc72300a3c
BLAKE2b-256 4253df652dc6aab9ba3d5e696384ce855008ae7bad6f76cba8f8d149db790b8c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page