A rate limiting module for Ellar
Project description
Ellar - Python ASGI web framework for building fast, efficient, and scalable RESTful APIs and server-side applications.
Introduction
A rate limit module for Ellar
Installation
$(venv) pip install ellar-throttler
Configure ThrottlerModule
We need to set up the ThrottlerModule
to be able for configuring throttling mechanisms for the entire application.
from ellar.common import Module
from ellar_throttler import AnonymousThrottler, ThrottlerModule, UserThrottler
@Module(
modules=(
ThrottlerModule.setup(
throttlers=[
AnonymousThrottler(limit=100, ttl=60*5), # 100 requests per 5mins
UserThrottler(limit=1000, ttl=60*60*24) # 1000 requests per day
]
),
)
)
class AppModule:
pass
Applying Throttling to Controllers
from ellar.common import Controller, get
from ellar_throttler import Throttle
from ellar.di import injectable
@injectable()
class AppService:
def success(self, use_auth: bool):
message = "success"
if use_auth:
message += " for Authenticated user"
return {message: True}
def ignored(self, use_auth: bool):
message = "ignored"
if use_auth:
message += " for Authenticated user"
return {message: True}
@Throttle(apply_interceptor=True)
@Controller("/limit")
class LimitController:
def __init__(self, app_service: AppService):
self.app_service = app_service
@get()
def get_throttled(self, use_auth: bool):
return self.app_service.success(use_auth)
@get("/shorter")
@Throttle(anon={"limit": 3, "ttl": 5}, user={"limit": 3, "ttl": 3}) # overriding anon and user throttler config
def get_shorter(self, use_auth: bool):
return self.app_service.success(use_auth)
References
License
Ellar is MIT licensed.
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
ellar_throttler-0.1.5.tar.gz
(10.2 kB
view hashes)
Built Distribution
Close
Hashes for ellar_throttler-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4079067d6c4a82a4cc2b9f2f9105a49a9a8efbf0e5e6c26e24eacceeebf6b6a0 |
|
MD5 | 93f5be9aa203c2d18d480dab2bcbedac |
|
BLAKE2b-256 | 483706c885703bb7d55d90613e64026121c64a29d678a228aa087df5a2b29355 |