fix(gateway): resolve unbounded DashMap memory leak in TokenBucketRateLimiter (#1609)#1610
Open
vivekrajsingh04 wants to merge 1 commit intomofa-org:mainfrom
Open
fix(gateway): resolve unbounded DashMap memory leak in TokenBucketRateLimiter (#1609)#1610vivekrajsingh04 wants to merge 1 commit intomofa-org:mainfrom
fix(gateway): resolve unbounded DashMap memory leak in TokenBucketRateLimiter (#1609)#1610vivekrajsingh04 wants to merge 1 commit intomofa-org:mainfrom
Conversation
…eLimiter with moka cache (mofa-org#1609)
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.
Description
This PR addresses a critical Denial of Service (DoS) vulnerability caused by an unbounded memory leak in the
TokenBucketRateLimiter.Previously,
TokenBucketRateLimitertracked rate limits using an unboundedDashMap. BecauseKeyStrategy::PerClientis supported, every unique client IP generated a permanent tracking entry in the system memory. Without any TTL or eviction mechanisms, long-running gateway instances were susceptible to encountering Out Of Memory (OOM) panics due to gradual accumulation or deliberate DoS attacks using mutating IP headers.Fix
DashMapformoka::sync::Cache: Replaced the unmanagedDashMapwith the highly concurrentmokacache interface.100,000concurrent clients per node, and stale trackers are garbage collected if they remain unaccessed for 10 minutes (long enough to let standard tokens refill, but short enough to prevent memory runaway).Performance Optimization (Zero Allocation)
As a bonus, I optimized
check_and_consume()to prevent heap string allocations. Previously, passing a client key necessitated a.to_string()allocation on every authenticated request before it reached the internalentry(). Now, it performs a zero-costself.buckets.get(key)first, allowing the gateway to breeze through repeated requests completely allocation-free.Fixes #1609
Type of Change
Testing
Arc<Mutex<TokenBucket>>state accurately caps limits globally under cross-thread contention.mokacache logic.