-
Notifications
You must be signed in to change notification settings - Fork 4
/
generator.go
127 lines (107 loc) · 2.93 KB
/
generator.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
package watchdog
import (
"math"
"runtime"
"sync/atomic"
"time"
)
type HeartbeatGeneratorFactory interface {
CreateGenerator(name string) *HeartbeatGenerator
}
func NewHeartbeatGenerator(id HeartbeatID, heartbeatPeriod time.Duration, out chan<- Heartbeat) HeartbeatGenerator {
return NewHeartbeatGeneratorWithRetries(id, heartbeatPeriod, 0, out)
}
func NewHeartbeatGeneratorWithRetries(id HeartbeatID, heartbeatPeriod time.Duration, retryCount uint8, out chan<- Heartbeat) HeartbeatGenerator {
attempts := retryCount
if out == nil {
attempts = 0
} else if attempts < math.MaxUint8 {
attempts++
}
period := uint32(0)
switch {
case heartbeatPeriod < 0:
panic("illegal value")
case heartbeatPeriod == 0:
break
case heartbeatPeriod <= time.Millisecond:
period = 1
default:
heartbeatPeriod /= time.Millisecond
if heartbeatPeriod > math.MaxUint32 {
period = math.MaxUint32
} else {
period = uint32(heartbeatPeriod)
}
}
return HeartbeatGenerator{id: id, heartbeatPeriod: period, sendAttempts: attempts, out: out}
}
type HeartbeatGenerator struct {
id HeartbeatID
heartbeatPeriod uint32
atomicNano int64
sendAttempts uint8
//name string
out chan<- Heartbeat
}
func (g *HeartbeatGenerator) Heartbeat() {
g.ForcedHeartbeat(false)
}
func (g *HeartbeatGenerator) ForcedHeartbeat(forced bool) {
lastNano := atomic.LoadInt64(&g.atomicNano)
if lastNano == DisabledHeartbeat {
//closed channel or generator
return
}
currentNano := time.Now().UnixNano()
if lastNano != 0 && !forced && currentNano-lastNano < int64(g.heartbeatPeriod)*int64(time.Millisecond) {
return
}
if !atomic.CompareAndSwapInt64(&g.atomicNano, lastNano, currentNano) {
return // there is no need to retry in case of contention
}
if g.send(Heartbeat{From: g.id, PreviousUnixTime: lastNano, UpdateUnixTime: currentNano}) {
return
}
atomic.CompareAndSwapInt64(&g.atomicNano, currentNano, lastNano) // try to roll-back on failed send
}
func (g *HeartbeatGenerator) Cancel() {
for {
lastNano := atomic.LoadInt64(&g.atomicNano)
if lastNano == DisabledHeartbeat {
//closed channel or generator
return
}
if atomic.CompareAndSwapInt64(&g.atomicNano, lastNano, DisabledHeartbeat) {
g.send(Heartbeat{From: g.id, PreviousUnixTime: lastNano, UpdateUnixTime: DisabledHeartbeat})
return // there is no need to retry in case of contention
}
}
}
func (g *HeartbeatGenerator) send(beat Heartbeat) bool {
defer func() {
err := recover() // just in case of the closed channel
if err != nil {
g.Disable()
}
}()
if g.sendAttempts == 0 {
return true
}
for i := g.sendAttempts; i > 0; i-- {
select {
case g.out <- beat:
return true
default:
// avoid lock up
runtime.Gosched()
}
}
return false
}
func (g *HeartbeatGenerator) Disable() {
atomic.StoreInt64(&g.atomicNano, DisabledHeartbeat)
}
func (g *HeartbeatGenerator) IsEnabled() bool {
return atomic.LoadInt64(&g.atomicNano) != DisabledHeartbeat
}