forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_checker.go
296 lines (247 loc) · 6.93 KB
/
host_checker.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
package main
import (
"bytes"
"github.com/jeffail/tunny"
"github.com/pmylund/go-cache"
"math/rand"
"net/http"
"time"
)
const (
defaultTimeout int = 10
defaultWorkerPoolSize int = 50
defaultSampletTriggerLimit int = 3
)
var HostCheckerClient *http.Client = &http.Client{Timeout: 500 * time.Millisecond}
type HostData struct {
CheckURL string
ID string
Method string
Headers map[string]string
Body string
MetaData map[string]string
}
type HostHealthReport struct {
HostData
ResponseCode int
Latency float64
IsTCPError bool
}
type HostUptimeChecker struct {
failureCallback func(HostHealthReport)
upCallback func(HostHealthReport)
pingCallback func(HostHealthReport)
workerPoolSize int
sampleTriggerLimit int
checkTimout int
HostList map[string]HostData
unHealthyList map[string]bool
pool *tunny.WorkPool
errorChan chan HostHealthReport
okChan chan HostHealthReport
stopPollingChan chan bool
sampleCache *cache.Cache
stopLoop bool
doResetList bool
newList *map[string]HostData
}
func (h *HostUptimeChecker) getStaggeredTime() time.Duration {
if h.checkTimout <= 5 {
return time.Duration(h.checkTimout) * time.Second
}
rand.Seed(time.Now().Unix())
min := h.checkTimout - 3
max := h.checkTimout + 3
dur := rand.Intn(max-min) + min
return time.Duration(dur) * time.Second
}
func (h *HostUptimeChecker) HostCheckLoop() {
for {
if h.stopLoop {
break
}
if h.doResetList {
if h.newList != nil {
h.HostList = *h.newList
h.newList = nil
h.doResetList = false
log.Debug("[HOST CHECKER] Host list reset")
}
}
for _, host := range h.HostList {
_, err := h.pool.SendWork(host)
if err != nil {
log.Error("[HOST CHECKER] could not send work, error: %v", err)
}
}
time.Sleep(h.getStaggeredTime())
}
log.Info("[HOST CHECKER] Checker stopped")
}
func (h *HostUptimeChecker) HostReporter() {
for {
select {
case okHost := <-h.okChan:
// Clear host from unhealthylist if it exists
found, _ := h.unHealthyList[okHost.ID]
if found {
h.upCallback(okHost)
delete(h.unHealthyList, okHost.ID)
}
go h.pingCallback(okHost)
case failedHost := <-h.errorChan:
cachedHostCount, found := h.sampleCache.Get(failedHost.ID)
if !found {
go h.sampleCache.Set(failedHost.ID, 1, cache.DefaultExpiration)
} else {
newVal := cachedHostCount.(int)
newVal += 1
go h.sampleCache.Set(failedHost.ID, newVal, cache.DefaultExpiration)
if newVal > h.sampleTriggerLimit {
log.Debug("[HOST CHECKER] [HOST WARNING]: ", failedHost.CheckURL)
// Reset the count
go h.sampleCache.Set(failedHost.ID, 1, cache.DefaultExpiration)
// track it
h.unHealthyList[failedHost.ID] = true
// Call the custom callback hook
go h.failureCallback(failedHost)
}
}
go h.pingCallback(failedHost)
case <-h.stopPollingChan:
log.Debug("[HOST CHECKER] Received kill signal")
break
}
}
}
func (h *HostUptimeChecker) CheckHost(toCheck HostData) {
log.Debug("[HOST CHECKER] Checking: ", toCheck.CheckURL, toCheck.ID)
t1 := time.Now()
var response *http.Response
var respErr error
useMethod := toCheck.Method
if toCheck.Method == "" {
useMethod = "GET"
}
var body = []byte(toCheck.Body)
req, err := http.NewRequest(useMethod, toCheck.CheckURL, bytes.NewBuffer(body))
if err != nil {
log.Error("Could not create request: ", err)
return
}
for header_name, header_value := range toCheck.Headers {
req.Header.Set(header_name, header_value)
}
req.Header.Set("Connection", "close")
response, respErr = HostCheckerClient.Do(req)
t2 := time.Now()
millisec := float64(t2.UnixNano()-t1.UnixNano()) * 0.000001
report := HostHealthReport{
HostData: toCheck,
Latency: millisec,
}
if respErr != nil {
report.IsTCPError = true
h.errorChan <- report
return
}
report.ResponseCode = response.StatusCode
if response.StatusCode != 200 {
h.errorChan <- report
return
}
// host is healthy, report it
h.okChan <- report
}
func (h *HostUptimeChecker) Init(workers, triggerLimit, timeout int, hostList map[string]HostData, failureCallback func(HostHealthReport), upCallback func(HostHealthReport), pingCallback func(HostHealthReport)) {
h.sampleCache = cache.New(30*time.Second, 5*time.Second)
h.stopPollingChan = make(chan bool)
h.errorChan = make(chan HostHealthReport)
h.okChan = make(chan HostHealthReport)
h.HostList = hostList
h.unHealthyList = make(map[string]bool)
h.failureCallback = failureCallback
h.upCallback = upCallback
h.pingCallback = pingCallback
h.workerPoolSize = workers
if workers == 0 {
h.workerPoolSize = defaultWorkerPoolSize
}
h.sampleTriggerLimit = triggerLimit
if triggerLimit == 0 {
h.sampleTriggerLimit = defaultSampletTriggerLimit
}
h.checkTimout = timeout
if timeout == 0 {
h.checkTimout = defaultTimeout
}
log.Debug("[HOST CHECKER] Config:TriggerLimit: ", h.sampleTriggerLimit)
log.Debug("[HOST CHECKER] Config:Timeout: ~", h.checkTimout)
log.Debug("[HOST CHECKER] Config:WorkerPool: ", h.workerPoolSize)
var pErr error
h.pool, pErr = tunny.CreatePool(h.workerPoolSize, func(hostData interface{}) interface{} {
input, _ := hostData.(HostData)
h.CheckHost(input)
return nil
}).Open()
log.Debug("[HOST CHECKER] Init complete")
if pErr != nil {
log.Error("[HOST CHECKER POOL] Error: %v\n", pErr)
}
}
func (h *HostUptimeChecker) Start() {
// Start the loop that checks for bum hosts
h.stopLoop = false
log.Debug("[HOST CHECKER] Starting...")
go h.HostCheckLoop()
log.Debug("[HOST CHECKER] Check loop started...")
go h.HostReporter()
log.Debug("[HOST CHECKER] Host reporter started...")
}
func (h *HostUptimeChecker) Stop() {
h.stopLoop = true
h.stopPollingChan <- true
log.Info("[HOST CHECKER] Stopping poller")
h.pool.Close()
}
func (h *HostUptimeChecker) AddHost(hd HostData) {
h.HostList[hd.ID] = hd
log.Info("[HOST CHECKER] Tracking: ", hd.ID)
}
func (h *HostUptimeChecker) RemoveHost(name string) {
delete(h.HostList, name)
log.Info("[HOST CHECKER] Stopped tracking: ", name)
}
func (h *HostUptimeChecker) ResetList(hostList *map[string]HostData) {
h.doResetList = true
h.newList = hostList
log.Debug("[HOST CHECKER] Checker reset queued!")
}
func hostcheck_example() {
// Create the poller
poller := &HostUptimeChecker{}
poller.Init(defaultWorkerPoolSize,
defaultSampletTriggerLimit,
defaultTimeout,
map[string]HostData{},
// On failure
func(fr HostHealthReport) {
log.Error("This host is failing: ", fr.CheckURL)
log.Info("---> Latency: \t", fr.Latency)
if fr.IsTCPError {
log.Info("---> TCP Error: \t", fr.IsTCPError)
} else {
log.Info("---> Response Code: \t", fr.ResponseCode)
}
},
// On success
func(fr HostHealthReport) {
log.Info("Host is back up! URL: ", fr.CheckURL)
},
func(fr HostHealthReport) {
log.Info("Host report, URL: ", fr.CheckURL)
})
// Start the check loop
poller.Start()
defer poller.Stop()
}