No project description provided
Project description
Robyn Rate Limits
This is an extension for Robyn that allows rate limiting your API.
Installation
You can get robyn-rate-limits
from PyPI, whichm means you can install it with pip easily:
python -m pip install robyn-rate-limits
If you would like to use Redis
as a store, enable the redis feature:
python -m pip install robyn-rate-limits[redis]
Usage
Define your API normally as you would with Robyn, and add a limiter middleware:
from robyn import Robyn
from robyn_rate_limits import InMemoryStore
from robyn_rate_limits import RateLimiter
app = Robyn(__file__)
limiter = RateLimiter(store=InMemoryStore, calls_limit=3, limit_ttl=100)
@app.before_request()
def middleware(request: Request):
return limiter.handle_request(app, request)
@app.get("/")
def h():
return "Hello, World!"
app.start(port=8080)
In the above example the store for the limits is in memory, to use Redis
, replace the limiter with:
from robyn_rate_limits import RedisStore
import redis
conn = redis.Redis(host='localhost', port=6379, db=0)
limiter = RateLimiter(store=RedisStore, calls_limit=2, limit_ttl=100, redis=conn)
Stores
Currently, both in-memory store and Redis
store are using a sliding window algorithm to store the calls.
The extension is designed in a way that you can implement your own store if you would like to use a different store or algorithm.
Identity
The identity of the client that the rate is limited by is automatically detected:
-
For endpoints that require authentication, the rate is enforced by token.
-
For endpoints that are open, the rate is enforced by IP.
How to contribute
If you add more stores or algorithms and would like them to be part of the official package you are more than welcomed to!
Please read the contributing guide for the guidelines.
Feel free to open issues if you have any question or suggestion.
Local development
-
Install the development dependencies:
poetry install --with dev
-
Install the pre-commit git hooks:
pre-commit install
-
Run
poetry run test_server
. This will run a server containing several examples of routes we use for testing purposes. You can see them attests/base_routes.py
. You can modify or add some to your likings.
You can then request the server you ran from an other terminal. Here is a GET
request done using curl for example:
curl http://localhost:8080/darwin/11/test
# 200
curl http://localhost:8080/darwin/11/test
# 200
curl http://localhost:8080/darwin/11/test
# 200
curl http://localhost:8080/darwin/11/test
# 429
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
File details
Details for the file robyn_rate_limits-0.2.0.tar.gz
.
File metadata
- Download URL: robyn_rate_limits-0.2.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.6 Linux/6.2.0-1015-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f696c3e9596f174d0a560b79193645fec50ed0bc5868ef079a7fe3fd481b1201 |
|
MD5 | 68f4f999cd44d4b47660af08035428e3 |
|
BLAKE2b-256 | 17426d4016e9a09873f756356ffda5603268d938942517219cb22b18186bd9c8 |
File details
Details for the file robyn_rate_limits-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: robyn_rate_limits-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.6 Linux/6.2.0-1015-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a7bf0b7aadff0f18462b9c9a43b33b80ec74683d332353f4a6b648258d67509 |
|
MD5 | 9179f11706adb2e82219992c4a8177c2 |
|
BLAKE2b-256 | acab92100683da1fa7508dbad100a0b61820c196e228142ad9a5c3ceb67dc421 |