All Projects → tomasbasham → Ratelimit

tomasbasham / Ratelimit

Licence: mit
API Rate Limit Decorator

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Ratelimit

caddy-ratelimit
HTTP rate limiting module for Caddy 2
Stars: ✭ 72 (-80.27%)
Mutual labels:  rate-limiting
Decorator
Function decorators for Elixir
Stars: ✭ 278 (-23.84%)
Mutual labels:  decorators
Ex rated
ExRated, the Elixir OTP GenServer with the naughty name that allows you to rate-limit calls to any service that requires it.
Stars: ✭ 328 (-10.14%)
Mutual labels:  rate-limiting
vue-decorator
Custom decorators to vue-class-component that fits Vue 3
Stars: ✭ 31 (-91.51%)
Mutual labels:  decorators
Rxweb
Tons of extensively featured packages for Angular, VUE and React Projects
Stars: ✭ 262 (-28.22%)
Mutual labels:  decorators
Sentinel
A powerful flow control component enabling reliability, resilience and monitoring for microservices. (面向云原生微服务的高可用流控防护组件)
Stars: ✭ 18,071 (+4850.96%)
Mutual labels:  rate-limiting
bind-observable
Provides a typescript decorator which binds class properties to observable companion properties.
Stars: ✭ 15 (-95.89%)
Mutual labels:  decorators
Cachier
Persistent, stale-free, local and cross-machine caching for Python functions.
Stars: ✭ 359 (-1.64%)
Mutual labels:  decorators
Helpful Decorators
Helpful decorators for typescript projects
Stars: ✭ 263 (-27.95%)
Mutual labels:  decorators
Warthog
GraphQL API Framework with strong conventions and auto-generated schema
Stars: ✭ 316 (-13.42%)
Mutual labels:  decorators
course-spring-microservices
Code examples built for the purpose of video course: Microservices With Spring Boot And Spring Cloud
Stars: ✭ 74 (-79.73%)
Mutual labels:  rate-limiting
kong-scalable-rate-limiter
Kong plugin for Rate Limiting at high throughputs.
Stars: ✭ 19 (-94.79%)
Mutual labels:  rate-limiting
Nestcloud
A NodeJS micro-service solution, writing by Typescript language and NestJS framework.
Stars: ✭ 290 (-20.55%)
Mutual labels:  decorators
validada
Another library for defensive data analysis.
Stars: ✭ 29 (-92.05%)
Mutual labels:  decorators
Ngx Auto Unsubscribe
Class decorator that will automatically unsubscribe from observables
Stars: ✭ 337 (-7.67%)
Mutual labels:  decorators
aequitas
Fairness regulator and rate limiter
Stars: ✭ 49 (-86.58%)
Mutual labels:  rate-limiting
Pebble
Multi threading and processing eye-candy.
Stars: ✭ 276 (-24.38%)
Mutual labels:  decorators
Ember Decorators
Useful decorators for Ember applications.
Stars: ✭ 360 (-1.37%)
Mutual labels:  decorators
Ng Metadata
Angular 2 decorators and utils for Angular 1.x
Stars: ✭ 356 (-2.47%)
Mutual labels:  decorators
Annon.api
Configurable API gateway that acts as a reverse proxy with a plugin system.
Stars: ✭ 306 (-16.16%)
Mutual labels:  rate-limiting

ratelimit |build| |maintainability|

APIs are a very common way to interact with web services. As the need to consume data grows, so does the number of API calls necessary to remain up to date with data sources. However many API providers constrain developers from making too many API calls. This is know as rate limiting and in a worst case scenario your application can be banned from making further API calls if it abuses these limits.

This packages introduces a function decorator preventing a function from being called more often than that allowed by the API provider. This should prevent API providers from banning your applications by conforming to their rate limits.

Installation

PyPi


Add this line to your application's requirements.txt:

.. code:: python

    ratelimit

And then execute:

.. code:: bash

    $ pip install -r requirements.txt

Or install it yourself:

.. code:: bash

    $ pip install ratelimit

GitHub

Installing the latest version from Github:

.. code:: bash

$ git clone https://github.com/tomasbasham/ratelimit
$ cd ratelimit
$ python setup.py install

Usage

To use this package simply decorate any function that makes an API call:

.. code:: python

from ratelimit import limits

import requests

FIFTEEN_MINUTES = 900

@limits(calls=15, period=FIFTEEN_MINUTES)
def call_api(url):
    response = requests.get(url)

    if response.status_code != 200:
        raise Exception('API response: {}'.format(response.status_code))
    return response

This function will not be able to make more then 15 API call within a 15 minute time period.

The arguments passed into the decorator describe the number of function invocation allowed over a specified time period (in seconds). If no time period is specified then it defaults to 15 minutes (the time window imposed by Twitter).

If a decorated function is called more times than that allowed within the specified time period then a ratelimit.RateLimitException is raised. This may be used to implement a retry strategy such as an expoential backoff <https://pypi.org/project/backoff/>_

.. code:: python

from ratelimit import limits, RateLimitException
from backoff import on_exception, expo

import requests

FIFTEEN_MINUTES = 900

@on_exception(expo, RateLimitException, max_tries=8)
@limits(calls=15, period=FIFTEEN_MINUTES)
def call_api(url):
    response = requests.get(url)

    if response.status_code != 200:
        raise Exception('API response: {}'.format(response.status_code))
    return response

Alternatively to cause the current thread to sleep until the specified time period has ellapsed and then retry the function use the sleep_and_retry decorator. This ensures that every function invocation is successful at the cost of halting the thread.

.. code:: python

from ratelimit import limits, sleep_and_retry

import requests

FIFTEEN_MINUTES = 900

@sleep_and_retry
@limits(calls=15, period=FIFTEEN_MINUTES)
def call_api(url):
    response = requests.get(url)

    if response.status_code != 200:
        raise Exception('API response: {}'.format(response.status_code))
    return response

License

This project is licensed under the MIT License <LICENSE.txt>_.

.. |build| image:: https://travis-ci.com/tomasbasham/ratelimit.svg?branch=master :target: https://travis-ci.com/tomasbasham/ratelimit

.. |maintainability| image:: https://api.codeclimate.com/v1/badges/21dc7c529c35cd7ef732/maintainability :target: https://codeclimate.com/github/tomasbasham/ratelimit/maintainability :alt: Maintainability

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].