Add request throttling, custom stream throttle and 404 guard middleware - #4837
Add request throttling, custom stream throttle and 404 guard middleware#4837Rachit7168 wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Sorry @Rachit7168, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
Reviewer's GuideAdds DRF-wide throttling defaults, introduces custom throttles for public stream/schedule and excessive 404s, wires a stricter throttle into the public stream endpoint, and adds middleware that rate-limits abusive 404 traffic using the shared Redis cache. Sequence diagram for 404 guard middleware with Excessive404ThrottlesequenceDiagram
actor Client
participant Django as DjangoApp
participant Block404Middleware
participant Cache as RedisCache
participant Excessive404Throttle
Client->>Django: HTTP request to unknown path
Django->>Client: 404 response
Client-->>Block404Middleware: process_response(request, response)
Block404Middleware->>Cache: get(404_counter:ip)
Cache-->>Block404Middleware: current_count
Block404Middleware->>Cache: set(404_counter:ip, count, timeout=60)
alt [count > MAX_404_PER_MINUTE]
Block404Middleware->>Excessive404Throttle: allow_request(request, view=None)
alt [allow_request returns False]
Excessive404Throttle->>Excessive404Throttle: wait()
Excessive404Throttle-->>Block404Middleware: retry_after_seconds
Block404Middleware-->>Client: HttpResponseTooManyRequests
else [allow_request returns True]
Block404Middleware-->>Client: original 404 response
end
else [count <= MAX_404_PER_MINUTE]
Block404Middleware-->>Client: original 404 response
end
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Thank you for contributingPlease complete the checklist. Screenshots in the PR description and completed AI reviews are checked automatically.
🤖 AI reviews❌ 2 failed or rate-limited AI reviewers
👤 Reviewer feedback👤
|
eea4a6f to
9b155e5
Compare
Close: #4602
Description
This PR introduces robust rate‑limiting for the public API and protects the server from abusive 404 scans.
What’s new
DEFAULT_THROTTLE_CLASSESandDEFAULT_THROTTLE_RATESare now defined inREST_FRAMEWORK.anon: 60 requests/minute (≈ 1 rps)user: 300 requests/minute (≈ 5 rps)public_stream: 10 requests/minute (the front‑end poll endpoint)public_schedule: 30 requests/minute-
excessive_404: 1 request/minute (used by custom middleware)Custom throttles (
app/eventyay/api/throttles.py)PublicStreamThrottle– applied toGET /rooms/{id}/streams/current/.PublicScheduleThrottle– placeholder for schedule‑related public endpoints.Excessive404Throttle– used by the 404‑guard middleware.ViewSet update (
app/eventyay/api/views/room.py) – thecurrent_streamaction now setsself.throttle_classes = [PublicStreamThrottle].404‑rate‑limit middleware (
app/eventyay/middleware/block_404.py)Retry‑Afterheader.eventyay.middleware.block_404.Block404Middleware).Redis cache already configured – the project uses
django.core.cache.backends.redis.RedisCache; the new middleware re‑uses the same cache, so throttling state is shared across all Gunicorn workers.Why this matters
Retry‑After) for both legit polling clients and abusive scanners.settings.py, no code changes needed to adjust rates.Verification steps
Retry‑After.Retry‑Afteris returned.Summary by Sourcery
Introduce API-wide rate limiting and 404 abuse protection using DRF throttling and custom middleware.
New Features: