-
Notifications
You must be signed in to change notification settings - Fork 11
/
window.go
49 lines (40 loc) · 928 Bytes
/
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
package ratelimiter
import (
"time"
)
// Window represents the structure of timing-window at given point of time.
type Window struct {
count uint64
startTime time.Time
}
func (w *Window) updateCount(n uint64) {
w.count += n
}
func (w *Window) getStartTime() time.Time {
return w.startTime
}
func (w *Window) setStateFrom(other *Window) {
w.count = other.count
w.startTime = other.startTime
}
func (w *Window) resetToTime(startTime time.Time) {
w.count = 0
w.startTime = startTime
}
func (w *Window) setToState(startTime time.Time, count uint64) {
w.startTime = startTime
w.count = count
}
// Creates and returns a pointer to the new Window instance.
//
// Parameters:
//
// 1. count: The initial count of the window.
//
// 2. startTime: The initial starting time of the window.
func NewWindow(count uint64, startTime time.Time) *Window {
return &Window{
count: count,
startTime: startTime,
}
}