in pkg/healthcheck/grpc.go [41:87]
func HealthCheck(addr string, enableTLS bool) int {
ctx := context.Background()
opts := []grpc.DialOption{
grpc.WithUserAgent("swctl_health_probe"),
grpc.WithBlock(),
}
if enableTLS {
// #nosec
creds := credentials.NewTLS(&tls.Config{
InsecureSkipVerify: true,
})
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithInsecure())
}
dialCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
conn, err := grpc.DialContext(dialCtx, addr, opts...)
if err != nil {
if err == context.DeadlineExceeded {
logger.Log.Printf("timeout: failed to connect service %q within 1 second", addr)
} else {
logger.Log.Printf("error: failed to connect service at %q: %+v", addr, err)
}
return ConnectionFailure
}
defer conn.Close()
rpcCtx, rpcCancel := context.WithTimeout(ctx, time.Second)
defer rpcCancel()
resp, err := healthpb.NewHealthClient(conn).Check(rpcCtx, &healthpb.HealthCheckRequest{Service: ""})
if err != nil {
if stat, ok := status.FromError(err); ok && stat.Code() == codes.DeadlineExceeded {
logger.Log.Printf("timeout: health request did not complete within 1 second")
} else {
logger.Log.Printf("error: health request failed: %+v", err)
}
return RPCFailure
}
if resp.GetStatus() != healthpb.HealthCheckResponse_SERVING {
logger.Log.Printf("OAP gRPC service is unhealthy %q", resp.GetStatus().String())
return Unhealthy
}
return Healthy
}