-
Notifications
You must be signed in to change notification settings - Fork 5
/
health_check.go
65 lines (55 loc) · 1.08 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
package gtm
import (
"log"
"time"
)
type HealthCheckMethod func(IP) bool
type DoNothingHealthCheck struct {
ips []IP
}
//Start empty start method
func (hk *DoNothingHealthCheck) Start() {}
//Receive empty
func (hk *DoNothingHealthCheck) Receive() []IP {
return hk.ips
}
type sleep func()
func sleepDuration(d time.Duration) sleep {
return func() {
time.Sleep(d)
}
}
type DefaultHealthCheck struct {
EndPoints []IP
healthEndpoints *[]IP
Frequency time.Duration
CheckHealth HealthCheckMethod
SleepFunc sleep
started bool
}
//Start Start HealthCheck
func (hk *DefaultHealthCheck) Start() {
if hk.started {
log.Println("Health Check already started")
return
}
hk.healthEndpoints = &hk.EndPoints
go func() {
for {
newEndpoint := make([]IP, 0)
for _, ip := range hk.EndPoints {
if hk.CheckHealth(ip) {
newEndpoint = append(newEndpoint, ip)
}
}
hk.healthEndpoints = &newEndpoint
hk.SleepFunc()
}
}()
hk.started = true
}
func (hk *DefaultHealthCheck) Receive() []IP {
var ips []IP
ips = *hk.healthEndpoints
return ips
}