Skip to main content

Rate limiting for callable functions

Project description

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

storage_uri = 'redis://ip:port/'
limiter = Limiter(
        storage_uri=storage_uri
    )

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://ip:port/'
)

@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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Function-Limiter-0.1.4.tar.gz (22.3 kB view hashes)

Uploaded Source

Built Distribution

Function_Limiter-0.1.4-py3-none-any.whl (6.6 kB view hashes)

Uploaded Python 3

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