-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
46 lines (38 loc) · 1.28 KB
/
status.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package component
import (
"sync/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
// Status indicates the health status of this component
type Status int
const (
// StatusHealthy indicates a healthy component
StatusHealthy Status = iota
// StatusUnhealthy indicates an unhealthy component
StatusUnhealthy
)
// GetStatus gets the health status of the component
func (c *Component) GetStatus() Status {
return Status(atomic.LoadInt64(&c.status))
}
// SetStatus sets the health status of the component
func (c *Component) SetStatus(status Status) {
atomic.StoreInt64(&c.status, int64(status))
if c.healthServer != nil {
switch status {
case StatusHealthy:
c.healthServer.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
case StatusUnhealthy:
c.healthServer.SetServingStatus("", healthpb.HealthCheckResponse_NOT_SERVING)
}
}
}
// RegisterHealthServer registers the component's health status to the gRPC server
func (c *Component) RegisterHealthServer(srv *grpc.Server) {
c.healthServer = health.NewServer()
healthpb.RegisterHealthServer(srv, c.healthServer)
}