/
health.go
72 lines (64 loc) · 1.86 KB
/
health.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package server
import (
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/cilium/pkg/hubble/relay/pool"
"github.com/cilium/cilium/pkg/inctimer"
"github.com/cilium/cilium/pkg/time"
)
type peerStatusReporter interface {
Status() pool.Status
}
type healthServer struct {
svc *health.Server
pm peerStatusReporter
probeInterval time.Duration
stopChan chan struct{}
}
// newHealthServer creates a new health server that monitors health
// using the provided status reporter
func newHealthServer(pm peerStatusReporter, probeInterval time.Duration) *healthServer {
svc := health.NewServer()
svc.SetServingStatus("", healthpb.HealthCheckResponse_NOT_SERVING)
svc.SetServingStatus(v1.ObserverServiceName, healthpb.HealthCheckResponse_NOT_SERVING)
hs := &healthServer{
svc: svc,
pm: pm,
probeInterval: probeInterval,
stopChan: make(chan struct{}),
}
return hs
}
// start starts the health server.
func (hs healthServer) start() {
check := func() {
st := hs.pm.Status()
if st.PeerServiceConnected && st.AvailablePeers > 0 {
hs.svc.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
hs.svc.SetServingStatus(v1.ObserverServiceName, healthpb.HealthCheckResponse_SERVING)
} else {
hs.svc.SetServingStatus("", healthpb.HealthCheckResponse_NOT_SERVING)
hs.svc.SetServingStatus(v1.ObserverServiceName, healthpb.HealthCheckResponse_NOT_SERVING)
}
}
go func() {
connTimer, connTimerDone := inctimer.New()
defer connTimerDone()
check()
for {
select {
case <-hs.stopChan:
return
case <-connTimer.After(hs.probeInterval):
check()
}
}
}()
}
// stop stops the health server.
func (hs healthServer) stop() {
close(hs.stopChan)
}