Skip to main content

drf-ratelimit-plus

PyPI version Python versions License: MIT

Rate limiting for Django REST Framework that goes beyond DRF's built-in UserRateThrottle/AnonRateThrottle: token bucket (with real burst handling), sliding window, fixed window, weighted request costs, per-plan rate tiers, and Redis Cluster support — built as real rest_framework.throttling.BaseThrottle subclasses, so Retry-After and the standard throttle lifecycle work exactly as DRF users already expect.

from drf_ratelimit_plus import rate_limit


class ArticleViewSet(viewsets.ModelViewSet):
    throttle_classes = [rate_limit(rate="100/m", algorithm="token_bucket", burst=20, key="user")]

Why

DRF's built-in throttles give you one fixed-window counter, one identity dimension (user or IP), and no burst tolerance. Real APIs need more: smooth burst handling that a fixed window can't provide, different limits per subscription tier, weighted costs for expensive endpoints, and a rate-limiting layer that scales horizontally against Redis Cluster without hot-key problems.

Features

  • Three algorithms: token bucket (smooth bursts against a steady refill rate), sliding window (approximate, memory-efficient, avoids fixed-window boundary bursts), and fixed window (simplest, cheapest).
  • Weighted costs: @ratelimit(rate="1000/h", cost=5) for an expensive endpoint sharing a budget with cheaper ones.
  • Plan tiers: different limits per subscription tier, resolved per-request.
  • Redis Cluster ready: every algorithm's Redis keys are designed for single-slot (or hash-tagged) atomic operations, so nothing requires cross-slot transactions.
  • Standard rate-limit headers: RateLimit-Limit, RateLimit-Remaining, RateLimit-Reset, and Retry-After on 429 — via DRF's own Throttled exception, so existing error-handling code keeps working.
  • Composable key functions: per IP, per user, per API key, per tenant, per endpoint, or any combination.
  • Dynamic configuration: rates can be a string, or a callable re-evaluated per request (e.g. reading a database-configured limit).
  • Fully typed, PEP 561 compatible, mypy --strict clean.

Installation

pip install drf-ratelimit-plus

Requires a Redis server (standalone or Cluster) — see docs/installation.md.

Quick Start

from drf_ratelimit_plus import rate_limit


class ArticleViewSet(viewsets.ModelViewSet):
    throttle_classes = [
        rate_limit(rate="100/m", algorithm="sliding_window", key="ip"),
    ]

Or the decorator form, for function-based views:

from rest_framework.decorators import api_view
from drf_ratelimit_plus import ratelimit


@api_view(["POST"])
@ratelimit(rate="10/m", key="user")
def expensive_action(request):
    ...

Documentation

Full documentation: https://mahmoudgshake.github.io/MahmoudPackages/drf-ratelimit-plus/

Contributing

Contributions are welcome — see CONTRIBUTING.md.

License

MIT — see LICENSE.

Metadata

Release files for drf-ratelimit-plus 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-ratelimit-plus 1.0.0
File Size Uploaded
drf_ratelimit_plus-1.0.0.tar.gz 44.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-ratelimit-plus 1.0.0
File Interpreter ABI Platform
drf_ratelimit_plus-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 68.6 kB

Release files / drf_ratelimit_plus-1.0.0.tar.gz

Download URL drf_ratelimit_plus-1.0.0.tar.gz
Size 44.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5c7aa4b745f8855d1779deb089503c6e3d9e4041736372ffa813dad66d7f9be3
BLAKE2b-256 checksum
How to use checksums
9a89f3d7673f4fc3a8a6e0b45e400fae71fac1b4c2dd611afe1aa28c498ccc10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / drf_ratelimit_plus-1.0.0-py3-none-any.whl

Download URL drf_ratelimit_plus-1.0.0-py3-none-any.whl
Size 24.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
07c709252ba671c2924d7bf8a961c1ff2c44dba4d05d8ac9964f275cda270a4e
BLAKE2b-256 checksum
How to use checksums
f731f812e68801b3805098398cf07ba993888feb01f1dc81855ca4accbeb4787
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page