-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed_window_counter.go
45 lines (37 loc) · 1.04 KB
/
fixed_window_counter.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
package ratelimiter
import (
"sync"
"time"
)
type FixedWindowCounter struct {
window_size int64 // size of the window in seconds
max_requests int // maximum number of requests per window
current_window int64 // current window time
request_count int // count of requests in the current window
mu sync.Mutex
}
func NewFixedWindowCounter(window_size int64, max_requests int) *FixedWindowCounter {
return &FixedWindowCounter{
window_size: window_size,
max_requests: max_requests,
current_window: time.Now().Unix() / window_size,
request_count: 0,
}
}
func (fxc *FixedWindowCounter) AllowRequest() bool {
fxc.mu.Lock()
defer fxc.mu.Unlock()
current_time := time.Now()
window := current_time.Unix() / fxc.window_size
// If we've moved to a new window, reset the counter
if window != fxc.current_window {
fxc.current_window = window
fxc.request_count = 0
}
// Check if we're still within the limit for this window
if fxc.request_count < fxc.max_requests {
fxc.request_count++
return true
}
return false
}