feat: implement adaptive rate limiting and throttling system#251
Merged
Conversation
|
@sonoflawal Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #176
Summary
Implements an adaptive rate limiting system that dynamically adjusts rate
limits based on real-time platform health signals, consumer behaviour, and
detected anomalies — complementing the existing static limits.
What's changed
Signal collection — samples CPU utilisation, DB pool utilisation, Redis
memory pressure, request queue depth, error rate, and p99 response time on a
configurable interval with rolling averages to smooth transient spikes
Adaptation modes — Normal → Elevated → Critical → Emergency, driven by
configurable per-signal thresholds with hysteresis to prevent oscillation
Consumer priority tiers — High (partners, microservices) are protected
from tightening in Elevated/Critical; Standard and Low receive progressively
aggressive multipliers; all tiers get a reduced but non-zero limit in Emergency
Per-consumer trend tracking — detects accelerating request rates and
applies an additional throttle multiplier during Elevated/Critical mode
Graceful degradation — non-essential endpoints return 503 with
Retry-Afterin Critical/Emergency; essential financial endpoints are queuedup to a configurable depth with FIFO shedding when full
Admin control —
GET/POST/DELETE /api/admin/adaptive-rate-limit/...endpoints for status inspection and manual mode override
Redis sync — effective multipliers are pushed to Redis on every cycle so
all application instances apply consistent limits simultaneously
Observability — Prometheus gauges for mode, all signal values, rolling
averages, and effective multipliers per tier; counters for mode transitions,
per-consumer throttle applications, and request shedding events; structured
log events with full signal context on every Critical/Emergency transition
Files
src/adaptive_rate_limit/migrations/20260403000000_adaptive_rate_limiting.sqlsrc/metrics/mod.rssrc/main.rssrc/lib.rstests/adaptive_rate_limit_integration.rsTesting
enforcement, multiplier application per tier, accelerating request detection,
request shedding logic, and endpoint classification
ordering, hysteresis, and worker lifecycle