https://github.com/python-ellar/ellar-throttler
A rate limiting module for Ellar
https://github.com/python-ellar/ellar-throttler
Last synced: 4 months ago
JSON representation
A rate limiting module for Ellar
- Host: GitHub
- URL: https://github.com/python-ellar/ellar-throttler
- Owner: python-ellar
- License: mit
- Created: 2023-02-26T14:30:19.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2025-02-01T00:57:13.000Z (12 months ago)
- Last Synced: 2025-09-29T14:58:46.494Z (4 months ago)
- Language: Python
- Size: 824 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Ellar - Python ASGI web framework for building fast, efficient, and scalable RESTful APIs and server-side applications.


[](https://badge.fury.io/py/ellar-throttler)
[](https://pypi.python.org/pypi/ellar-throttler)
[](https://pypi.python.org/pypi/ellar-throttler)
## Introduction
A rate limit module for Ellar
## Installation
```shell
$(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.
```python
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
```python
from ellar.common import Controller, get
from ellar_throttler import Throttle, AnonymousThrottler, UserThrottler
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(intercept=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)
@get("/shorter-inline-throttling")
@Throttle(AnonymousThrottler(ttl=5, limit=3), UserThrottler(ttl=3, limit=3)) # overriding global throttling options
def get_shorter_inline_version(self, use_auth: bool):
return self.app_service.success(use_auth)
```
## References
- [Documentation](https://python-ellar.github.io/ellar/techniques/rate-limit)
## License
Ellar is [MIT licensed](LICENSE).
