Fast ratelimit implementation with django caches
Project description
django-fast-ratelimit
Django-fast-ratelimit provides a secure and fast ratelimit facility based on the django caching framework.
Installation
pip install django-fast-ratelimit
Note: pip >= 19 is required, I use the novel pyproject.toml method
usage
Decorator:
import ratelimit
@ratelimit.decorate(key="ip", rate="1/s")
def expensive_func(request):
# how many ratelimits request limiting
if request.ratelimit["request_limit"] > 0:
# reschedule with end of rate epoch
return request_waiting(request.ratelimit["end"])
blocking Decorator (raises RatelimitError):
import ratelimit
@ratelimit.decorate(key="ip", rate="1/s", block=True, methods=ratelimit.UNSAFE)
def expensive_func(request):
# how many ratelimits request limiting
if request.ratelimit["end"] > 0:
decorate View (requires group):
import ratelimit
from django.views.generic import View
from django.utils.decorators import method_decorator
@method_decorator(ratelimit.decorate(
key="ip", rate="1/s", block=True, methods=ratelimit.SAFE, group="required"
), name="dispatch")
class FooView(View):
...
manual
import ratelimit
def func(request):
ratelimit.get_ratelimit(key="ip", rate="1/s", request=request, group="123")
# or only for GET
ratelimit.get_ratelimit(
key="ip", rate="1/s", request=request, group="123", methods="GET"
)
# also simple calls possible (note: key in bytes format)
ratelimit.get_ratelimit(
key=b"abc", rate="1/s", group="123"
)
TODO
- more documentation
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
Built Distribution
Close
Hashes for django-fast-ratelimit-0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79883d176a567b9e3812e6714399087732b879d6e2473f993cb41106d94080a1 |
|
MD5 | 665caffd9c77e70de48b2e53f039755b |
|
BLAKE2b-256 | 45cc1c496978a7cdf72b9cf8143cc46b684ecbf33c312ee60744bf480f4a3462 |
Close
Hashes for django_fast_ratelimit-0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd324c6d935423980c30c4ff88b38e51d6aded2ed00a11cff90434dbbacb0611 |
|
MD5 | f3c4ee48f3a37b6ce86520842b6ce792 |
|
BLAKE2b-256 | 63d53c38f2fa3bb2d02b1795f2fee02c9661ced6d0afedfcb65a1447ed2fcbdb |