GithubHelp home page GithubHelp logo

aiopylimit's Introduction

Build Status

aiopylimit

A distributed rate limiting library for python using leaky bucket algorithm and Redis supporting asyncio

Prerequisites

This library makes use of Redis and needs it as a prerequisite. Redis sentinel is also supported. Make sure that Redis server is running before using this library

Installation

pip install aiopylimit

Example

For this example, let's assume that redis hostname is 'localhost' and port number is 6379 Suppose you want to limit the number of api calls to be 1000 per minute. It can be done by the following steps.

1.) Import the library

from aiopylimit import AIOPyRateLimit

2.) Initialize the library

AIOPyRateLimit.init(redis_host="localhost", redis_port=6379)

3.) Create a rate limit namespace

limit = AIOPyRateLimit(60, 100)     # rate limit period in seconds
                                    # no of attempts in the time period

4.) Record ant attempt and check if it is allowed or not

is_allowed = await limit.attempt('api_count')   # will return true if number of attempts
                                          # are less than or equal to 1000 in last 1 minute,
                                          # false otherwise

5.) In order to check if a namespace is already rate limited or not

is_rate_limited = await limit.is_rate_limited('api_count')  # will return true if this namespace is already rate limited, false otherwise

Reference

https://engineering.classdojo.com/blog/2015/02/06/rolling-rate-limiter/

Based on https://github.com/biplap-sarkar/pylimit

aiopylimit's People

Contributors

biplap-sarkar avatar dmarkey avatar zoni avatar

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.