Rate-limiting for django
Project description
django-ratelimiter
Rate limiting for django using limits.
Documentation: https://django-ratelimiter.readthedocs.io
Installation
pip install django-ratelimiter
Usage
By default django-ratelimiter
will use the default cache.
Django configuration
To use a non-default cache define DJANGO_RATELIMITER_CACHE
in settings.py
.
# Set up django caches
CACHES = {
"custom-cache": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
}
}
# "default" cache is used if setting is not defined.
DJANGO_RATELIMITER_CACHE = "custom-cache"
Any storage backend provided by limits
package can also be used by defining DJANGO_RATELIMITER_STORAGE
:
from limits.storage import RedisStorage
DJANGO_RATELIMITER_STORAGE = RedisStorage(uri="redis://localhost:6379/0")
For more details on storages refer to limits documentation.
Rate limiting strategies
- Fixed window
- Fixed Window with Elastic Expiry
- Moving Window - Only supported with
limits
storage by settingDJANGO_RATELIMITER_STORAGE
View decorator
By default all requests are rate limited
from django_ratelimiter import ratelimit
@ratelimit("5/minute")
def view(request: HttpRequest) -> HttpResponse:
return HttpResponse("OK")
Pick a rate limiting strategy, default is fixed-window
:
# options: fixed-window, fixed-window-elastic-expiry, moving-window
@ratelimit("5/minute", strategy="fixed-window-elastic-expiry")
def view(request: HttpRequest) -> HttpResponse:
return HttpResponse("OK")
You can define per-user limits using request attribute key.
@ratelimit("5/minute", key="user")
def view(request: HttpRequest) -> HttpResponse:
return HttpResponse("OK")
Callable key can be used to define more complex rules:
@ratelimit("5/minute", key=lambda r: r.user.username)
def view(request: HttpRequest) -> HttpResponse:
return HttpResponse("OK")
Rate-limit only certain methods:
@ratelimit("5/minute", methods=["POST", "PUT"])
def view(request):
return HttpResponse("OK")
Provide a custom response:
from django.http import HttpResponse
@ratelimit("5/minute", response=HttpResponse("Too many requests", status=400))
def view(request):
return HttpResponse("OK")
Using non-default storage:
from limits.storage import RedisStorage
@ratelimit("5/minute", storage=RedisStorage(uri="redis://localhost:6379/0"))
def view(request):
return HttpResponse("OK")
DRF/ninja/class-based views
django-ratelimiter
is framework-agnostic, it should work with DRF/ninja out of the box.
Class-based views are also supported with method_decorator.
See examples in test_app.
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
Built Distribution
Hashes for django_ratelimiter-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 912538d6921aa67f6c52a8b0bdac5c8ee0b2cf7657741f5ba4c8106d278bc30f |
|
MD5 | e511bae4979690d41508067736b7db84 |
|
BLAKE2b-256 | 0ed5faf0d1fbfe508513a2d3ad3373b459267e8a0a9a3575c9bc7ade1d88166d |