Skip to main content

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

  1. Install the development dependencies: poetry install --with dev

  2. Install the pre-commit git hooks: pre-commit install

  3. Run poetry run test_server. This will run a server containing several examples of routes we use for testing purposes. You can see them at tests/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


Download files

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

Source Distribution

robyn_rate_limits-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

robyn_rate_limits-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file robyn_rate_limits-0.1.0.tar.gz.

File metadata

  • Download URL: robyn_rate_limits-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.4 Linux/5.15.0-1041-azure

File hashes

Hashes for robyn_rate_limits-0.1.0.tar.gz
Algorithm Hash digest
SHA256 866d916d67672619b62419bb0c30e929fbc82c7093bc39add27bdffdd4fb343b
MD5 1323b52872c01e45b04ba0b6650924a0
BLAKE2b-256 b5124ede75488ee5b5b56bf0866c96148ca4ad0e4f8817f6dc6f20820b42f067

See more details on using hashes here.

File details

Details for the file robyn_rate_limits-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: robyn_rate_limits-0.1.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.4 Linux/5.15.0-1041-azure

File hashes

Hashes for robyn_rate_limits-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e4828968746e067399d4022a0e82917063b87be1ec1943918786c6cff358e71e
MD5 9d45302a98a7080cc0beb07f10462546
BLAKE2b-256 391accc62aa54cee21e62ef11d69a1eb164d1f5f42f625d0a3e7e3d1dc4d3408

See more details on using hashes here.

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