-
Notifications
You must be signed in to change notification settings - Fork 3
/
health_check.go
214 lines (188 loc) · 5.56 KB
/
health_check.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
package t1k
import (
"time"
)
type HealthCheckConfig struct {
Interval int64 // default 1s
HealthThreshold int64 // default 5
UnhealthThreshold int64 // default 3
Addresses []string // like ['1.1.1.1:80', '1.1.1.2:8000']
Timeout int64 // default 3000 millisecond
HealthCheckProtocol string
EnableTLS bool
}
type HealthCheckStats struct {
Count uint64
ErrorCount int64
Panic bool
LatestErrorInfo string
Status string
}
type HealthCheckService struct {
healthCheckConfig *HealthCheckConfig
Stats *HealthCheckStats
exitChan chan bool
configChan chan *HealthCheckConfig
}
const (
HealthCheckRunningStatus = "running"
HealthCheckStoppedStatus = "stopped"
)
// IsHealth return health check result
func (hcs *HealthCheckService) IsHealth() bool {
if hcs.Stats.ErrorCount > hcs.healthCheckConfig.UnhealthThreshold {
return false
}
// from unhealth to health
if hcs.Stats.ErrorCount < 0 {
return false
}
if hcs.Stats.Panic {
return false
}
return true
}
// HealthDetailInfo return health check result with detail info
func (hcs *HealthCheckService) HealthDetailInfo() string {
return hcs.Stats.LatestErrorInfo
}
// HealthCheckStats return health check stats
func (hcs *HealthCheckService) HealthCheckStats() HealthCheckStats {
return *hcs.Stats
}
// UpdateConfig trigger the health check or update health check config
func (hcs *HealthCheckService) UpdateConfig(config *HealthCheckConfig) error {
healthCheck := &HealthCheckConfig{}
if config.Interval <= 0 {
healthCheck.Interval = 1
} else {
healthCheck.Interval = config.Interval
}
if config.UnhealthThreshold <= 0 {
healthCheck.UnhealthThreshold = 3
} else {
healthCheck.UnhealthThreshold = config.UnhealthThreshold
}
if config.HealthThreshold <= 0 {
healthCheck.HealthThreshold = 5
} else {
healthCheck.HealthThreshold = config.HealthThreshold
}
if config.Timeout <= 0 {
healthCheck.Timeout = 3000
}
healthCheck.Addresses = config.Addresses
healthCheck.HealthCheckProtocol = config.HealthCheckProtocol
healthCheck.EnableTLS = config.EnableTLS
hcs.configChan <- healthCheck
return nil
}
// current support t1k protocol
func (hcs *HealthCheckService) GetHealthCheckProtocol() string {
return hcs.healthCheckConfig.HealthCheckProtocol
}
func (hcs *HealthCheckService) CaclErrorCount(ok bool, info string) {
// unhealth | health unhealth
// ____________________0____________x______________->
//- UnhealthThreshold +
// ErrorCount == 0 is health
// 0 < ErrorCount <= UnhealthThreshold is health
// ErrorCount > UnhealthThreshold is unhealth
// ErrorCount < 0 is unhealth
if !ok {
// already in unhealth, reset
if hcs.Stats.ErrorCount < 0 {
hcs.Stats.ErrorCount = -hcs.healthCheckConfig.HealthThreshold
} else {
hcs.Stats.ErrorCount += 1
hcs.Stats.LatestErrorInfo = info
if hcs.Stats.ErrorCount > hcs.healthCheckConfig.UnhealthThreshold {
// in unhealth
hcs.Stats.ErrorCount = -hcs.healthCheckConfig.HealthThreshold
}
}
} else {
// from unhealth to health
if hcs.Stats.ErrorCount < 0 {
hcs.Stats.ErrorCount += 1
} else if hcs.Stats.ErrorCount-1 < 0 { // health
hcs.Stats.ErrorCount = 0
} else {
hcs.Stats.ErrorCount = 0 //health, reset
}
hcs.Stats.LatestErrorInfo = ""
}
}
func (hcs *HealthCheckService) ClearStats() {
hcs.Stats.Count = 0
hcs.Stats.ErrorCount = 0
hcs.Stats.LatestErrorInfo = ""
hcs.Stats.Panic = false
hcs.Stats.Status = HealthCheckStoppedStatus
}
// Run start a health check go routine.
// If you want health check enable, need invoke UpdateConfig to trigger.
func (hcs *HealthCheckService) Run() error {
defer func() {
if r := recover(); r != nil {
// panic need rerun NewHealthCheckService to recover
hcs.Stats.Panic = true
hcs.Stats.Status = HealthCheckStoppedStatus
}
}()
for {
config := <-hcs.configChan
if hcs.healthCheckConfig != nil {
// only single Run instance
return nil
}
hcs.healthCheckConfig = config
if hcs.healthCheckConfig != nil {
break
}
}
rerun:
hcs.ClearStats()
hcs.Stats.Status = HealthCheckRunningStatus
// init protocol instance
var protocolIns HCProtocol
switch hcs.healthCheckConfig.HealthCheckProtocol {
case HEALTH_CHECK_T1K_PROTOCOL:
protocolIns = NewT1KProtocol(hcs.healthCheckConfig.Addresses, hcs.healthCheckConfig.Timeout)
case HEALTH_CHECK_HTTP_PROTOCOL:
protocolIns = NewHTTPProtocol(hcs.healthCheckConfig.Addresses, hcs.healthCheckConfig.Timeout, hcs.healthCheckConfig.EnableTLS)
default:
protocolIns = NewT1KProtocol(hcs.healthCheckConfig.Addresses, hcs.healthCheckConfig.Timeout)
}
tricker := time.NewTicker(time.Duration(hcs.healthCheckConfig.Interval) * time.Second)
for {
select {
case <-tricker.C:
hcs.Stats.Count += 1
ok, info := protocolIns.Check()
hcs.CaclErrorCount(ok, info)
case config := <-hcs.configChan:
hcs.healthCheckConfig = config
goto rerun
case <-hcs.exitChan:
hcs.ClearStats()
return nil
}
}
}
func (hcs *HealthCheckService) Close() {
hcs.exitChan <- true
close(hcs.exitChan)
close(hcs.configChan)
}
// NewHealthCheckService create new HealthCheckService for health check.
// After create new health check service, invoke UpdateConfig to update health check
func NewHealthCheckService() (*HealthCheckService, error) {
healthCheckStats := &HealthCheckStats{}
healthCheckService := &HealthCheckService{
Stats: healthCheckStats,
configChan: make(chan *HealthCheckConfig, 1),
exitChan: make(chan bool, 1),
}
return healthCheckService, nil
}