Rate limiting for callable functions
Project description
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
Changelog
v0.1.1
- Release Date: 2021-02-21
- Bug Fixed:
Multiple line decorator doesn’t work
v0.1.0
- Release Date: 2021-02-20
Set Custom Database name
- Bug Fixed:
Using per in limitation doesn’t work.
Default keys(limitations, key, exempt) doesn’t set
v0.0.12
- Release Date: 2021-02-18
Exempt key added.
v0.0.11
- Release Date: 2021-02-15
Global limitations key added.
v0.0.10
- Release Date: 2021-02-14
Global limitations added.
v0.0.9
- Release Date: 2021-01-28
Rate limit policy changed to successful call.
- Bug Fixed:
Multiple limit doesn’t work.
v0.0.8
- Release Date: 2021-01-17
- Bug Fixed:
Null key bugs Fixed.
v0.0.7
- Release Date: 2021-01-17
Bug Fixed.
v0.0.6
- Release Date: 2021-01-16
Added redis as in memory storage.
v0.0.5
- Release Date: 2021-01-16
Added garbage collector.
v0.0.4
- Release Date: 2021-01-16
Added Validator to limiter.
v0.0.3
- Release Date: 2021-01-14
Added callable function for limitation and key function.
v0.0.2
- Release Date: 2021-01-14
Bug fixed.
v0.0.1
- Release Date: 2021-01-13
Basic Function Limiter.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for Function_Limiter-0.1.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6947d0b3e6871fec9a75adea3b785b770fe5c8fcc7f871fb9594f477531aa0c |
|
MD5 | 9e54bd787c86499d91faae02c0c87581 |
|
BLAKE2b-256 | ab434200dfada26658dd34621580d9d43973d5249792cbf81b9bc72ebcb2bd8d |