Skip to content

Commit 3255704

Browse files
committed
Add comments to sliding_window.go
1 parent fa384f5 commit 3255704

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

internal/algorithm/sliding_window.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
// These have no Redis dependency and exist to make the logic unit-testable in isolation.
2+
// Production traffic goes through the Lua scripts in scripts/lua/ which run atomically on Redis.
13
package algorithm
24

35
import (
46
"sync"
57
"time"
68
)
79

10+
// SlidingWindow is an in-memory sliding window counter.
11+
// Not safe for distributed use — use the Redis Lua implementation for multi-replica deployments.
812
type SlidingWindow struct {
913
mu sync.Mutex
1014
requests []time.Time
@@ -23,6 +27,7 @@ func (sw: *SlidingWindow) Allow() bool {
2327
now := time.Now()
2428
cutoff := now.Add(-sw.window)
2529

30+
// Evict expired entries
2631
valid := sw.requests[:0]
2732
for _, t := range sw.requests {
2833
if t.After(cutoff) {

0 commit comments

Comments
 (0)