feat: TTL-based eviction for RateLimiter (issue #6)#11
Merged
Conversation
…ry growth Adds NewRateLimiterWithTTL constructor and an Evict() method that removes entries not seen within the configured TTL. Also exposes Len() for observability and testing. Callers using the zero-TTL path see no behavior change. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds TTL-aware tracking to internal/security’s per-client RateLimiter to mitigate unbounded growth by making idle entries evictable.
Changes:
- Add
NewRateLimiterWithTTL(rpm, burst, ttl)constructor and store per-client entries withlastSeen. - Add
Evict()to remove entries idle longer thanttl, andLen()for observability/testing. - Extend rate limiter tests to cover eviction behavior and
Len().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| internal/security/ratelimit.go | Introduces TTL-aware entry tracking plus Evict()/Len() APIs. |
| internal/security/ratelimit_test.go | Adds tests for eviction semantics and length reporting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| // NewRateLimiter creates a RateLimiter with the given requests-per-minute and burst size. | ||
| // Entries are never evicted unless Evict is called explicitly or a background ticker calls it. |
Comment on lines
26
to
33
| func NewRateLimiter(rpm, burst int) *RateLimiter { | ||
| return NewRateLimiterWithTTL(rpm, burst, 0) | ||
| } | ||
|
|
||
| // NewRateLimiterWithTTL creates a RateLimiter that considers entries older than ttl as | ||
| // evictable. Set ttl = 0 to disable eviction. | ||
| func NewRateLimiterWithTTL(rpm, burst int, ttl time.Duration) *RateLimiter { | ||
| return &RateLimiter{ |
Comment on lines
+60
to
+73
| // Evict removes entries that have not been seen for longer than the configured TTL. | ||
| // No-op when TTL is zero. | ||
| func (rl *RateLimiter) Evict() { | ||
| if rl.ttl <= 0 { | ||
| return | ||
| } | ||
| cutoff := time.Now().Add(-rl.ttl) | ||
| rl.mu.Lock() | ||
| defer rl.mu.Unlock() | ||
| for id, e := range rl.entries { | ||
| if e.lastSeen.Before(cutoff) { | ||
| delete(rl.entries, id) | ||
| } | ||
| } |
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
NewRateLimiterWithTTL(rate, burst, ttl)constructorEvict()method that removes limiter entries idle longer thanttlLen()for observability and test assertionsTest plan
TestRateLimiter_EvictsIdleEntries— entries past TTL are removedTestRateLimiter_LenAfterEviction—Len()reflects post-eviction countTestRateLimiter_ActiveEntryNotEvicted— recently-used entries survive evictionCloses #6
🤖 Generated with Claude Code