-
Notifications
You must be signed in to change notification settings - Fork 26
/
health.go
52 lines (41 loc) · 1.24 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
package cc
import (
"context"
"fmt"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
// ServiceHealthStatus adopted from grpc-health-probe cli implementation
// https://github.com/grpc-ecosystem/grpc-health-probe/blob/master/main.go.
func ServiceHealthStatus(addr, service string) bool {
connTimeout := time.Second * 30
rpcTimeout := time.Second * 30
bCtx := context.Background()
dialCtx, dialCancel := context.WithTimeout(bCtx, connTimeout)
defer dialCancel()
dialOpts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
conn, err := grpc.DialContext(dialCtx, addr, dialOpts...) //nolint: staticcheck
if err != nil {
return false
}
defer conn.Close()
rpcCtx, rpcCancel := context.WithTimeout(bCtx, rpcTimeout)
defer rpcCancel()
if err := Retry(rpcTimeout, time.Millisecond*100, func() error {
resp, err := healthpb.NewHealthClient(conn).Check(rpcCtx, &healthpb.HealthCheckRequest{Service: service})
if err != nil {
return err
}
if resp.GetStatus() != healthpb.HealthCheckResponse_SERVING {
return fmt.Errorf("gRPC endpoint not SERVING")
}
return nil
}); err != nil {
return false
}
return true
}