Rate Limit
this package help you
to limit the call's [frequent] rates of any callable in python.
- default built-in memory is :
expiring dict memorythat not recommended for production usage
Usage
basic usage
from src.rlimit import Limiter, Per
from time import sleep
@Limiter(times=2, per=2 * Per.SECOND)
def func_a():
return True
func_a() # True
func_a() # True
func_a() # OverflowError
sleep(2)
func_a() # True
func_a() # True
func_a() # OverflowError
key example
from src.rlimit import Limiter, Per
@Limiter(
times=2, per=2 * Per.SECOND,
key=lambda name, age: str(hash((name, age)))
)
def func_a(name: str, age: int):
return f'hi2{name}-{age}'
func_a('gzuz', 13)
func_a('gzuz', 14)
func_a('gzuz', 13)
func_a('gzuz', 15)
func_a('gzuz', 14)
func_a('gzuz', 14) # OverflowError
func_a('gzuz', 13) # OverflowError
fastapi
from src.rlimit import Limiter, Per
from fastapi import APIRouter, HTTPException, Request
router = APIRouter(...)
@router.get(...)
@Limiter(
times=2, per=4 * Per.SECOND,
key=lambda r: r.client.host,
exception=HTTPException(429)
)
def test_ratelimit(request: Request):
return 'no limit'
Install
pip install git+https://github.com/omidekz/rlimit
pypi will add
Custom Memory
to implement new memory class,
you have to extend BaseMemory abstract class
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
rlimit-1.5.tar.gz
(15.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
rlimit-1.5-py3-none-any.whl
(16.0 kB
view details)
File details
Details for the file rlimit-1.5.tar.gz.
File metadata
- Download URL: rlimit-1.5.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/4.0.2 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41cfb7f2918ce2daabeb87086443d60b68ada1079f303e9777c99c4e5fdea48b
|
|
| MD5 |
1a17ddf00edbc360391ebf25b3135816
|
|
| BLAKE2b-256 |
18bf35723971368311025569612d498d4acf5df9c0d0adce5451e9e96baf2ba6
|
File details
Details for the file rlimit-1.5-py3-none-any.whl.
File metadata
- Download URL: rlimit-1.5-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/4.0.2 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1a36b6719de25cd2399c65a45811218d52ef0d1847921d2771bee97100c4aea
|
|
| MD5 |
48b1c0f8191e23a35d9a0446a557afa8
|
|
| BLAKE2b-256 |
f31ad87fa0bc6c94498ac6ee24176d5b48c66fb4e342c0da910e1908effcfc35
|