GithubHelp home page GithubHelp logo

mghorbani2357 / function-limiter Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 2.0 136 KB

Function Limiter designed to limit the call rate of callable function in python. It can be used in Flask, Django, WebSocket, etc.

License: MIT License

Python 100.00%
python python3 limiter

function-limiter's Introduction

Function-Limiter

GitHub Licence build GitHub Workflow Status codecov quality coverage downloadrate downloads PyPI PyPI - Format PyPI - Wheel GitHub last commit GitHub Release Date

Function-Limiter provides call rate limitation of callable function.

Installation

pip install Function-Limiter

Quick Start

Add the rate limiter to your function as decorator. The following example uses the default in memory implementation. Limiter() create instance of limiter. By using limiter.limit() call rate of callable function become limited. limiter.limit(limitation, key) limitation get the limitation can be assigned number per one of these keywords (second, minute, hour, day, month, year). Limitation applied on defined key.

from function_limiter.limiter import Limiter
from function_limiter.limiter import RateLimitExceeded
import time

limiter = Limiter()


@limiter.limit('3/second', 'key')
def function():
    print('hello world!')
from function_limiter.limiter import Limiter
from function_limiter.limiter import RateLimitExceeded
import time
import redis

limiter = Limiter(
        storage_uri=redis.Redis()
    )

There are a few ways of using this decorator depending on your preference and use-case.

Single decorator

The limit string can be a single limit or a delimiter separated string

@limiter.limit('3/second;10 per minute', 'key')
def function():
    print('hello world!')

Custom keying function

You can implement your own function to retrieve the value of rate limit config.

def limitation():
    return '5/second'

def key():
    return 'custom key'

@limiter.limit(limitation, key=key)
def function():
    print('hello world!')

Redis storage

Redis storage can be involved to lunch multiple instance of application.

limiter = Limiter(
    storage_uri=redis.Redis()
)

@limiter.limit('3/minute', 'key')
def func():
    pass

Exempt key

Exempt key can be used to exempt defined keys. If key and exempt key matched it ignores the limitations

limiter = Limiter()

@limiter.limit('3/minute', 'key', exempt='key')
def func():
    pass

Default values

You can define rate limit default value when the Limiter instance was initialized. By defining default rate limit values if there isn't any value for the specific key it applies the default value.

limiter = Limiter(
    default_limitations='3/minute',
    default_key='key',
    default_exempt='key'
)

@limiter.limit()
def func():
    pass

Limitation reset

Limitation can be reset for specific key.

limiter = Limiter()

@limiter.limit('3 per second', 'key')
def func():
    pass

for _ in range(3):
   func()

limiter.reset('key')

for _ in range(3):
   func()

Asynchronous function limit

Limitation can be reset for specific key.

limiter = Limiter()

@limiter.limit('3 per second', 'key')
async def func():
    pass

for _ in range(3):
   func()

limiter.reset('key')

for _ in range(3):
   func()

function-limiter's People

Contributors

codacy-badger avatar ed-xcf avatar mghorbani2357 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

ed-xcf bellyfat

function-limiter's Issues

Day period is not work

The regex string in __validate_limitations not contain 'day'. I open a pull request to fix it.
Your "Function-Limiter" is very cool!

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.