Skip to content

Instantly share code, notes, and snippets.

@dchw
Created July 26, 2022 00:18
Show Gist options
  • Save dchw/53b4bf1e5e87cf952f6d0cc8d7582219 to your computer and use it in GitHub Desktop.
Save dchw/53b4bf1e5e87cf952f6d0cc8d7582219 to your computer and use it in GitHub Desktop.
Prototype streaming healthcheck?
func monitorHealth(ctx context.Context, cc *grpc.ClientConn, cancelConn func()) {
defer cancelConn()
defer cc.Close()
healthClient := grpc_health_v1.NewHealthClient(cc)
wc, err := healthClient.Watch(ctx, &grpc_health_v1.HealthCheckRequest{})
if err != nil {
bklog.G(ctx).
WithError(err).
Error("could not start healthcheck stream")
return
}
for {
bklog.G(ctx).Debugf("awaiting healthcheck")
resp, err := wc.Recv()
if err != nil {
errStatus, _ := status.FromError(err)
switch errStatus.Code() {
case codes.Unavailable:
bklog.G(ctx).
WithError(err).
Debug("healthcheck returned unavaliable, client probably just exited?")
default:
bklog.G(ctx).
WithError(err).WithField("status", errStatus.String()).
WithField("status_code", errStatus.Code()).
Error("healthcheck error")
}
return
}
if resp.Status != grpc_health_v1.HealthCheckResponse_SERVING {
bklog.G(ctx).
WithError(err).
WithField("health_status", resp.Status).
Warn("bad healthcheck status")
return
}
bklog.G(ctx).Debugf("successful healthcheck recieved")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment