-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuffer_period.go
296 lines (249 loc) · 5.97 KB
/
buffer_period.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package hcat
import (
"context"
"sync"
"time"
)
// timers is a threadsafe object to manage multiple timers that represent
// buffer periods for objects by their ID.
type timers struct {
mux sync.RWMutex
// timers is the map of IDs to their active buffer timers.
timers map[string]*timer
buffered map[string]bool
ch chan string
}
// timer is an internal representation of a single buffer state.
type timer struct {
mux sync.RWMutex
id string
ch chan string
deadline time.Time
min time.Duration
max time.Duration
timer timerer
newTimerer func(d time.Duration) timerer
cancelTick context.CancelFunc
isActive bool
}
// time.Timer interface to allow mocking for testing without races
type timerer interface {
Reset(time.Duration) bool
GetC() <-chan time.Time
Stop() bool
}
func newTimers() *timers {
return &timers{
timers: make(map[string]*timer),
buffered: make(map[string]bool),
ch: make(chan string, 10),
}
}
// Run is a blocking function to monitor timers and notify the channel
// a buffer period has completed.
func (t *timers) Run(triggerCh chan string) {
for {
id, ok := <-t.ch
if !ok {
return
}
t.mux.Lock()
t.buffered[id] = true
t.mux.Unlock()
triggerCh <- id
}
}
// Stop sends a signal to halt monitoring of timers and clears out any active
// timers.
func (t *timers) Stop() {
t.mux.Lock()
defer t.mux.Unlock()
for id, timer := range t.timers {
timer.stop()
delete(t.timers, id)
}
}
// Add a new timer and returns if the timer was added.
func (t *timers) Add(min, max time.Duration, id string) bool {
t.mux.Lock()
defer t.mux.Unlock()
if t.timers[id] != nil {
return false
}
t.timers[id] = newTimer(t.ch, min, max, id)
return true
}
// Buffered checks the cache of recently expired timers if the timer is done
// buffering. (used in testing)
func (t *timers) Buffered(id string) bool {
t.mux.RLock()
defer t.mux.RUnlock()
return t.buffered[id]
}
// isBuffering tests whether buffing is currently in use
func (t *timers) isBuffering(id string) bool {
_, ok := t.timers[id]
return ok
}
// tick activates the buffer period and updates the timer.
// Returns false if no timer is found.
func (t *timers) tick(id string) bool {
return t._tick(id, time.Now())
}
// tick with 'now' passed in to allow testing
func (t *timers) _tick(id string, now time.Time) bool {
t.mux.Lock()
defer t.mux.Unlock()
timer, ok := t.timers[id]
if ok {
timer.tick(now)
}
return ok
}
// Reset resets an active timer
func (t *timers) Reset(id string) {
t.mux.Lock()
defer t.mux.Unlock()
if timer, ok := t.timers[id]; ok {
timer.reset()
delete(t.buffered, id)
}
}
// add timer using test version of time.Timer
func (t *timers) testAdd(min, max time.Duration, id string) bool {
ok := t.Add(min, max, id)
if ok {
t.timers[id].newTimerer = NewTestTimer
}
return ok
}
// returns the timer for id
func (t *timers) get(id string) *timer {
t.mux.Lock()
defer t.mux.Unlock()
if timer, ok := t.timers[id]; ok {
return timer
}
return nil
}
// //////////////////////////////////////////////////////////////////////
// newTimer creates a new buffer timer for the given template.
func newTimer(ch chan string, min, max time.Duration, id string) *timer {
return &timer{
id: id,
min: min,
max: max,
ch: ch,
// change to use test timer in tests
newTimerer: NewRealTimer,
}
}
func (t *timer) stop() {
if t.timer != nil {
t.timer.Stop()
}
}
// tick updates the minimum buffer timer
func (t *timer) tick(now time.Time) {
if t.active() {
t.activeTick(now)
return
}
t.inactiveTick(now)
}
func (t *timer) active() bool {
t.mux.RLock()
defer t.mux.RUnlock()
return t.isActive
}
// inactiveTick is the first tick of a buffer period, set up the timer and
// calculate the max deadline.
func (t *timer) inactiveTick(now time.Time) {
if t.timer == nil {
t.timer = t.newTimerer(t.min)
} else {
t.timer.Reset(t.min)
}
ctx, cancel := context.WithCancel(context.Background())
t.mux.Lock()
t.isActive = true
t.deadline = now.Add(t.max) // reset the deadline ot the future
t.cancelTick = cancel
t.mux.Unlock()
go func(ctx context.Context) {
select {
case <-ctx.Done():
return
case <-t.timer.GetC():
t.mux.Lock()
t.ch <- t.id
t.isActive = false
t.mux.Unlock()
}
}(ctx)
}
// activeTick snoozes the timer for the min time, or snooze less if we are coming
// up against the max time.
func (t *timer) activeTick(now time.Time) {
// Wait for the lock in case the go routine is updating the active state.
// If the timer has already fired don't snooze and let the next tick reset
// the buffer period to active
t.mux.Lock()
defer t.mux.Unlock()
if !t.isActive {
return
}
if now.Add(t.min).Before(t.deadline) {
t.timer.Reset(t.min)
} else if dur := t.deadline.Sub(now); dur > 0 {
t.timer.Reset(dur)
}
}
// reset resets the timer to inactive
func (t *timer) reset() {
t.mux.Lock()
defer t.mux.Unlock()
if !t.isActive {
return
}
t.cancelTick()
t.isActive = false
}
// //////////////////////////////////////////////////////////////////////
// time.Timer wrapper and a mocked/test Timer implementation
// They both meet the `timerer` interface above
// time.Timer wrapped to fit the interface (needed a getter for the channel)
type realTimer struct {
time.Timer
}
func NewRealTimer(d time.Duration) timerer {
return &realTimer{*time.NewTimer(d)}
}
func (tt *realTimer) GetC() <-chan time.Time {
return tt.C
}
// time.Timer for testing where it totals up the timer time for comparison
type testTimer struct {
C chan time.Time
totalTime time.Duration
}
// testTimer
func NewTestTimer(d time.Duration) timerer {
C := make(chan time.Time)
return &testTimer{C: C, totalTime: d}
}
func (tt *testTimer) GetC() <-chan time.Time {
return tt.C
}
func (tt *testTimer) Reset(d time.Duration) bool {
tt.totalTime += d
return true
}
func (tt *testTimer) Stop() bool {
return true
}
func (tt *testTimer) send() {
tt.C <- time.Now()
}