Replace unbounded ConcurrentHashMap with bounded Caffeine cache in RateLimitingFilter#54
Open
vaquarkhan wants to merge 1 commit intospring-ai-community:mainfrom
Open
Conversation
…in RateLimitingFilter RateLimitingFilter stored per-client rate limit buckets in a ConcurrentHashMap that never evicted entries. Every unique client IP permanently allocated a Bucket object that was never removed, causing unbounded heap growth behind load balancers, CDNs, or under attack until OutOfMemoryError. Replace ConcurrentHashMap with a bounded Caffeine cache (default: 100,000 max entries, 5-minute expiry after last access). Caffeine is already a transitive dependency via Spring Boot; added as explicit dependency in runtime-starter pom.xml. Fixes: RateLimitingFilter unbounded memory leak
Collaborator
maschnetwork
left a comment
There was a problem hiding this comment.
Thanks for spotting this. A few minor comments. Can you also please add a test for bounded buckets and eviction?
| private final ConcurrentHashMap<String, Bucket> buckets = new ConcurrentHashMap<>(); | ||
| private static final long DEFAULT_MAX_BUCKETS = 100_000; | ||
|
|
||
| private static final Duration DEFAULT_BUCKET_EXPIRY = Duration.ofMinutes(5); |
| private static final String UTF_8 = "UTF-8"; | ||
|
|
||
| private final ConcurrentHashMap<String, Bucket> buckets = new ConcurrentHashMap<>(); | ||
| private static final long DEFAULT_MAX_BUCKETS = 100_000; |
Collaborator
There was a problem hiding this comment.
Should we have a lower default here? We assume to have buckets per Agentcore Runtime session.
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.
Summary
RateLimitingFilterstores per-client rate limit buckets in aConcurrentHashMapthat never evicts entries. Every unique client IP permanently allocates aBucketobject (~200-500 bytes) that is never removed. Behind a load balancer, CDN, or under attack, this causes unbounded heap growth untilOutOfMemoryError.Changes
spring-ai-agentcore-runtime-starter/pom.xml— Added explicitcaffeinedependency (already a transitive dependency via Spring Boot)RateLimitingFilter.java— ReplacedConcurrentHashMap<String, Bucket>with a boundedCaffeine Cache<String, Bucket>(default: 100,000 max entries, 5-minute expiry after last access)Before vs After
Backward Compatibility
ThrottleConfigurationor existing testsTesting
mvn test -pl spring-ai-agentcore-runtime-starter— 80 tests, 0 failuresmvn spring-javaformat:apply— cleanFixes #51