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, which 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)

Stores

Robyn Rate Limits supports multiple caching strategies to suit different use cases, both in-memory and Redis-based:

In-Memory Stores

1. Sliding Window (Default)

from robyn_rate_limits import InMemoryStore

limiter = RateLimiter(store=InMemoryStore, calls_limit=3, limit_ttl=100)

2. Fixed Window

from robyn_rate_limits import InMemoryFixedWindowStore

limiter = RateLimiter(store=InMemoryFixedWindowStore, calls_limit=3, limit_ttl=100, window_size=60)

3. Token Bucket

from robyn_rate_limits import InMemoryTokenBucketStore

limiter = RateLimiter(store=InMemoryTokenBucketStore, calls_limit=3, refill_rate=1, capacity=5)

Redis-based Stores

To use Redis-based stores, first establish a Redis connection:

import redis

redis_conn = redis.Redis(host='localhost', port=6379, db=0)

1. Redis Sliding Window

from robyn_rate_limits import RedisStore

limiter = RateLimiter(store=RedisStore, calls_limit=3, limit_ttl=100, redis=redis_conn)

2. Redis Fixed Window

from robyn_rate_limits import RedisFixedWindowStore

limiter = RateLimiter(store=RedisFixedWindowStore, calls_limit=3, limit_ttl=100, window_size=60, redis=redis_conn)

3. Redis Token Bucket

from robyn_rate_limits import RedisTokenBucketStore

limiter = RateLimiter(store=RedisTokenBucketStore, calls_limit=3, refill_rate=1.0, capacity=5, redis=redis_conn)

Choose the appropriate store based on your application's needs:

  • Use the Sliding Window for a balance between accuracy and performance.
  • Use Fixed Window for simplicity and when precise timing isn't critical.
  • Use Token Bucket when you want to allow short bursts of traffic.
  • Use Redis-based stores for distributed systems or when you need persistence.

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.3.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

robyn_rate_limits-0.3.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: robyn_rate_limits-0.3.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.10 Linux/6.8.0-1014-azure

File hashes

Hashes for robyn_rate_limits-0.3.0.tar.gz
Algorithm Hash digest
SHA256 b3cd619c13783ff65a8471e0e3f3127acbfbbe1a617e8297c0e91f0a5277d8df
MD5 599be263c54ea1bead6423999a512809
BLAKE2b-256 05a021e29bd522ccceeb2570160c6f376472eb2c01cc751e683692c5b6dea6c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: robyn_rate_limits-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.10 Linux/6.8.0-1014-azure

File hashes

Hashes for robyn_rate_limits-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c462631feddfab65070a7bf77fb257de4ff580256111bfd7cfb5ba660d10404c
MD5 4fbeb0ac716c4807100b7cb6a0b1fa57
BLAKE2b-256 d74cdd260e2358a5a87cf6229fbf221d6556bd1f6a7964479cbda6a334318658

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