-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsliding_window.go
56 lines (47 loc) · 1.38 KB
/
sliding_window.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ratelimiter
import (
"sync"
"time"
)
type SlidingWindowCounter struct {
rate float64
interval time.Duration
currentCount int
previousCount int
windowStart time.Time
mu sync.Mutex
timeProvider TimeProvider
}
func NewSlidingWindowCounter(rate float64, interval time.Duration, tp TimeProvider) *SlidingWindowCounter {
return &SlidingWindowCounter{
rate: rate,
interval: interval,
currentCount: 0,
previousCount: 0,
windowStart: tp.Now(),
timeProvider: tp,
}
}
func (swc *SlidingWindowCounter) Allow() bool {
swc.mu.Lock()
defer swc.mu.Unlock()
// 1. Check if we're in a new window, if so shift the counts
now := swc.timeProvider.Now()
elapsed := now.Sub(swc.windowStart)
if elapsed >= swc.interval {
// Move currentCount to previousCount, reset currentCount, adjust windowStart
swc.previousCount += swc.currentCount
swc.currentCount = 0
swc.windowStart = now
}
// 2. Calculate the weighted count based on the position in the current window
elapsedFraction := elapsed.Seconds() / swc.interval.Seconds()
weightedCount := max(0, float64(swc.previousCount)*(1.0-elapsedFraction)+float64(swc.currentCount))
// 3. If the weighted count is less than the rate, increment the current count and return true
if weightedCount < swc.rate {
swc.currentCount++
return true
}
// 4. Otherwise, return false
return false
}