Giter Club home page Giter Club logo

python-redis-rate-limit's Introduction

python-redis-rate-limit

https://travis-ci.org/EvoluxBR/python-redis-rate-limit.svg?branch=master

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, 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: 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'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.