Skip to main content

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

Metadata

Release files for Function-Limiter 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Function-Limiter 0.2.0
File Size Uploaded
Function-Limiter-0.2.0.tar.gz 22.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Function-Limiter 0.2.0
File Interpreter ABI Platform
Function_Limiter-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.1 kB

Release files / Function-Limiter-0.2.0.tar.gz

Download URL Function-Limiter-0.2.0.tar.gz
Size 22.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8b42aae485c980c2de718c47497e4739973f6c8dc08a788b222b808b8b28de28
BLAKE2b-256 checksum
How to use checksums
eb3057d00852ef4d6c3d6991b073d1b27f0b48e3be2a8172964f5d66ef058691
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.5

Release files / Function_Limiter-0.2.0-py3-none-any.whl

Download URL Function_Limiter-0.2.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
48ec7d649469113c9a6754581e7c76076f85e8c64547a19918df1ee9bbf00c50
BLAKE2b-256 checksum
How to use checksums
fb69ab535f2e8f327eea0c8a6abd77ddc8f1c90e70f8dc7bff44ec07c418c9fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.4

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

3 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.6.2

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page