We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fa384f5 commit 3255704Copy full SHA for 3255704
1 file changed
internal/algorithm/sliding_window.go
@@ -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.
3
package algorithm
4
5
import (
6
"sync"
7
"time"
8
)
9
10
+// SlidingWindow is an in-memory sliding window counter.
11
+// Not safe for distributed use — use the Redis Lua implementation for multi-replica deployments.
12
type SlidingWindow struct {
13
mu sync.Mutex
14
requests []time.Time
@@ -23,6 +27,7 @@ func (sw: *SlidingWindow) Allow() bool {
23
27
now := time.Now()
24
28
cutoff := now.Add(-sw.window)
25
29
30
+ // Evict expired entries
26
31
valid := sw.requests[:0]
32
for _, t := range sw.requests {
33
if t.After(cutoff) {
0 commit comments