Skip to main content

Python Rate Limiter based on Redis.

Project description

https://travis-ci.org/EvoluxBR/python-redis-rate-limit.svg?branch=master 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.5+

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

python_redis_rate_limit-0.0.8-py2.py3-none-any.whl (5.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file python_redis_rate_limit-0.0.8-py2.py3-none-any.whl.

File metadata

  • Download URL: python_redis_rate_limit-0.0.8-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.2

File hashes

Hashes for python_redis_rate_limit-0.0.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9022b21eb49f63c09b52f2423dc2001968cb0d0483b1b106821ce75e4cc376e0
MD5 a9e943306868b72a7630290443ec0674
BLAKE2b-256 7a7639687719a5e82b7f1c663d42d1b5829a548569e642467d8ca4826e7ac591

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