Skip to content

feat: TTL-based eviction for RateLimiter (issue #6)#11

Merged
pythondatascrape merged 1 commit into
mainfrom
fix/ratelimiter-eviction
May 6, 2026
Merged

feat: TTL-based eviction for RateLimiter (issue #6)#11
pythondatascrape merged 1 commit into
mainfrom
fix/ratelimiter-eviction

Conversation

@pythondatascrape
Copy link
Copy Markdown
Owner

Summary

  • Adds NewRateLimiterWithTTL(rate, burst, ttl) constructor
  • Adds Evict() method that removes limiter entries idle longer than ttl
  • Adds Len() for observability and test assertions
  • Zero-TTL path is unchanged — no breaking changes

Test plan

  • TestRateLimiter_EvictsIdleEntries — entries past TTL are removed
  • TestRateLimiter_LenAfterEvictionLen() reflects post-eviction count
  • TestRateLimiter_ActiveEntryNotEvicted — recently-used entries survive eviction
  • All existing rate-limiter tests still pass

Closes #6

🤖 Generated with Claude Code

…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>
Copilot AI review requested due to automatic review settings May 6, 2026 14:17
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with lastSeen.
  • Add Evict() to remove entries idle longer than ttl, and Len() 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)
}
}
@pythondatascrape pythondatascrape merged commit 30d4664 into main May 6, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: RateLimiter leaks memory — no eviction of idle client entries

2 participants