Skip to content

Adding sliding window options#8

Merged
serroba merged 1 commit into
mainfrom
dev
Jan 1, 2026
Merged

Adding sliding window options#8
serroba merged 1 commit into
mainfrom
dev

Conversation

@serroba
Copy link
Copy Markdown
Owner

@serroba serroba commented Jan 1, 2026

Add sliding window option

Copilot AI review requested due to automatic review settings January 1, 2026 04:59
@serroba serroba merged commit c8a7915 into main Jan 1, 2026
6 checks passed
@serroba serroba deleted the dev branch January 1, 2026 05:01
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

This PR adds a sliding window rate limiting strategy to complement the existing fixed window implementation. A sliding window rate limiter tracks individual request timestamps and expires them individually, providing more granular rate limiting compared to the fixed window approach which resets all requests at window boundaries.

Key Changes:

  • Implements SlidingLimiter with timestamp-based request tracking and automatic cleanup
  • Adds comprehensive test coverage for concurrent access, window expiry, and edge cases
  • Integrates sliding window strategy into the registry test suite alongside existing strategies

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
window/sliding.go Core sliding window limiter implementation with queue-based timestamp tracking and memory optimization
window/sliding_test.go Test suite covering basic functionality, concurrency safety, window expiry behavior, and partial expiry scenarios
registry/registry_test.go Integration of SlidingWindowConfig into the registry test infrastructure to validate compatibility with other rate limiting strategies

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread window/sliding.go
l.head = 0
}

if len(l.q)-l.head+1 > int(l.limit) {
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

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

The limit check logic is incorrect. The condition len(l.q)-l.head+1 > int(l.limit) adds 1 to the current count before checking, which causes the limiter to reject requests one too early. For example, with a limit of 2, this rejects the second request instead of the third.

The correct logic should check if the current count (without the +1) is already at or above the limit: len(l.q)-l.head >= int(l.limit).

Suggested change
if len(l.q)-l.head+1 > int(l.limit) {
if len(l.q)-l.head >= int(l.limit) {

Copilot uses AI. Check for mistakes.
Comment thread registry/registry_test.go
Comment on lines +69 to +72
type SlidingWindowConfig struct {
limit uint32
duration time.Duration
}
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

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

The struct field naming is inconsistent with other config types in this file. TokenBucketConfig, LeakyBucketConfig, and FixedWindowConfig all use exported (capitalized) field names, but SlidingWindowConfig uses unexported (lowercase) fields 'limit' and 'duration'. This breaks the established naming pattern.

The fields should be renamed to 'Limit' and 'Duration' (or 'Window' to match FixedWindowConfig) for consistency.

Copilot uses AI. Check for mistakes.
Comment thread registry/registry_test.go
TokenBucketConfig{Capacity: capacity, Rate: rate},
LeakyBucketConfig{Capacity: capacity, Rate: rate},
FixedWindowConfig{Limit: capacity, Window: win},
SlidingWindowConfig{limit: capacity, duration: win},
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

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

The struct instantiation uses lowercase field names while other config types in the same function use capitalized field names. This inconsistency occurs because SlidingWindowConfig has unexported fields.

Once the SlidingWindowConfig fields are properly exported, this line should be updated to use capitalized field names: SlidingWindowConfig{Limit: capacity, Duration: win} (or Window: win if that field name is chosen).

Copilot uses AI. Check for mistakes.
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.

2 participants