/
control_timer.go
73 lines (63 loc) · 1.71 KB
/
control_timer.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
package node
import (
"math/rand"
"time"
)
type timerFactory func(time.Duration) <-chan time.Time
//ControlTimer struct
type ControlTimer struct {
timerFactory timerFactory
tickCh chan struct{} //sends a signal to listening process
resetCh chan time.Duration //receives instruction to reset the heartbeatTimer
stopCh chan struct{} //receives instruction to stop the heartbeatTimer
shutdownCh chan struct{} //receives instruction to exit Run loop
set bool
}
//NewControlTimer is a ControlTimer factory method
func NewControlTimer(timerFactory timerFactory) *ControlTimer {
return &ControlTimer{
timerFactory: timerFactory,
tickCh: make(chan struct{}),
resetCh: make(chan time.Duration),
stopCh: make(chan struct{}),
shutdownCh: make(chan struct{}),
}
}
//NewRandomControlTimer is a ControlTimer factory method that produces a ControlTimer with a random timeout
func NewRandomControlTimer() *ControlTimer {
randomTimeout := func(min time.Duration) <-chan time.Time {
if min == 0 {
return nil
}
extra := (time.Duration(rand.Int63()) % min)
return time.After(min + extra)
}
return NewControlTimer(randomTimeout)
}
//Run starts the Control Timer
func (c *ControlTimer) Run(init time.Duration) {
setTimer := func(t time.Duration) <-chan time.Time {
c.set = true
return c.timerFactory(t)
}
timer := setTimer(init)
for {
select {
case <-timer:
c.tickCh <- struct{}{}
c.set = false
case t := <-c.resetCh:
timer = setTimer(t)
case <-c.stopCh:
timer = nil
c.set = false
case <-c.shutdownCh:
c.set = false
return
}
}
}
//Shutdown shuts down the ControlTimer
func (c *ControlTimer) Shutdown() {
close(c.shutdownCh)
}