-
Notifications
You must be signed in to change notification settings - Fork 9
/
kong_healthcheck.go
80 lines (65 loc) · 1.55 KB
/
kong_healthcheck.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
package main
import (
"log"
"strconv"
"sync"
"time"
)
type kongHealthCheckConfig struct {
healthCheckPath string
healthCheckInterval string
}
type kongHealthCheck struct {
ticker *time.Ticker
targetChan chan target
client Client
wg sync.WaitGroup
}
func newKongHealthCheck(targetChan chan target, client Client, hcConfig *kongHealthCheckConfig) (*kongHealthCheck, error) {
hcInterval, err := strconv.Atoi(hcConfig.healthCheckInterval)
if err != nil {
return nil, err
}
return &kongHealthCheck{
ticker: time.NewTicker(time.Millisecond * time.Duration(hcInterval)),
client: client,
targetChan: targetChan,
}, nil
}
func (khc *kongHealthCheck) start() {
timeChan := khc.ticker.C
for range timeChan {
khc.monitorHealthOfTargets(khc.targetChan)
}
return
}
func (khc *kongHealthCheck) stop() {
khc.wg.Wait()
close(khc.targetChan)
khc.ticker.Stop()
}
func (khc *kongHealthCheck) monitorHealthOfTargets(targetChan chan target) {
upstreams, err := khc.client.upstreams()
if err != nil {
log.Printf("failed to fetch upstreams: %s", err)
return
}
for _, u := range upstreams {
khc.wg.Add(1)
go func(u upstream) {
defer khc.wg.Done()
khc.fetchAndQueueTargetsFor(u.ID, targetChan)
}(u)
}
return
}
func (khc *kongHealthCheck) fetchAndQueueTargetsFor(upstreamID string, targetChan chan target) {
targets, err := khc.client.targetsFor(upstreamID)
if err != nil {
log.Printf("failed to fetch targets for upstream %s: %s", upstreamID, err)
return
}
for _, target := range targets {
targetChan <- target
}
}