Skip to main content

Rate-limiting for django

Project description

django-ratelimiter

PyPI version CI codecov Python Versions license docs

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

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")

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

django_ratelimiter-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

django_ratelimiter-0.1.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file django_ratelimiter-0.1.1.tar.gz.

File metadata

  • Download URL: django_ratelimiter-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for django_ratelimiter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0025a354b6052aad2e5b8510c0c4c6ffde1e62e7813c5cc741e3625c7dcee6f0
MD5 fe2c3d927671ae113011d7da8f3cbdae
BLAKE2b-256 14c7c17405ac0f2e09bb4079abb14ac88b30f4d36ea89d8e53e4bbdf02c6b5c7

See more details on using hashes here.

File details

Details for the file django_ratelimiter-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ratelimiter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e74068e216f247f47760bd8eddcef4d727134394eea0bae10c6c74af49be1b23
MD5 8cf395ba5da4b3cdfa15d5f2b5214653
BLAKE2b-256 1102c29cee408e7568c39ba1471ca7366ed43a8c3f1db03d017a5a03bb4ab9c9

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