feat: add tenant-aware token bucket rate limiter - #733
Open
onyekachi66 wants to merge 1 commit into
Open
Conversation
|
@onyekachi66 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 #659
Background & Context
Currently, single high-volume regional liquidity pools can overwhelm our processing queues (Issue #649). Because telemetry processing capacity was not strictly isolated per tenant/asset feed, an influx of requests from one region could degrade or entirely exhaust the throughput available to other regions. We needed an equitable distribution mechanism to guarantee that each configured asset feed maintains its fair share of processing bandwidth, even under load spikes from neighbors.
What Does This PR Do?
This PR introduces a thread-safe
TokenBucketRateLimiterclass configured to regulate request flow on a per-tenant basis.Token Bucket Algorithm Implementation:
TokenBucketRateLimitertosrc/queue/backpressure.py.capacity(maximum burst allowed) andrefill_rate(sustained token regeneration per second).threading.Lock()to ensure safe concurrent operations in asynchronous environments.time.monotonic()to evade continuous background loop overhead.retry_after_smetric when requests are blocked, enabling downstream clients/queues to pause or delay processing intelligently rather than dropping data outright.Equitable Tenant Distribution:
tenant_iduniquely, meaning exhaustion of tokens for one tenant completely shields all other tenants operating well within their limits.Automated Verification:
pytestcases intests/test_backpressure.py.test_multitenant_token_bucket: Validates strict tenant isolation—tenant A can be fully blocked while tenant B operates normally.test_token_bucket_refill: Asserts that mathematical token regeneration strictly adheres to the configuredrefill_rateover fractional seconds.Impact & Expected Outcomes
Testing Instructions for Reviewers
To verify this behavior locally, you can run the following test suite targeting the multitenant and refill functionality:
All tests are confirmed green and pass locally.
Related Issues
Closes #659