Skip to content

Commit 428f099

Browse files
committed
refactor grpc health check and add comments
1 parent 31afe47 commit 428f099

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

packages/orchestrator/internal/server/healthcheck.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ func (h *Healthcheck) Start(ctx context.Context, listener net.Listener) {
8181
}
8282
}
8383

84+
// report updates the health status.
85+
// This function is run in a goroutine every healthcheckFrequency for the reason of having
86+
// longer running tasks that might me too slow or resource intensive to be run
87+
// in the healthcheck http handler directly.
8488
func (h *Healthcheck) report(ctx context.Context) error {
8589
h.mu.Lock()
8690
defer h.mu.Unlock()
@@ -91,25 +95,32 @@ func (h *Healthcheck) report(ctx context.Context) error {
9195
// Update last run on report
9296
h.lastRun = time.Now()
9397

94-
// Report health
95-
c, err := h.grpcHealth.Check(childCtx, &healthpb.HealthCheckRequest{
98+
// Report health of the gRPC
99+
var err error
100+
h.status, err = h.getGRPCHealth(childCtx)
101+
if err != nil {
102+
return err
103+
}
104+
105+
return nil
106+
}
107+
108+
// getGRPCHealth returns the health status of the grpc.Server by calling the health service check.
109+
func (h *Healthcheck) getGRPCHealth(ctx context.Context) (Status, error) {
110+
c, err := h.grpcHealth.Check(ctx, &healthpb.HealthCheckRequest{
96111
// Empty string is the default service name
97112
Service: "",
98113
})
99114
if err != nil {
100-
h.status = Unhealthy
101-
102-
return err
115+
return Unhealthy, err
103116
}
104117

105118
switch c.GetStatus() {
106119
case healthpb.HealthCheckResponse_SERVING:
107-
h.status = Healthy
120+
return Healthy, nil
108121
default:
109-
h.status = Unhealthy
122+
return Unhealthy, nil
110123
}
111-
112-
return nil
113124
}
114125

115126
type HealthResponse struct {

0 commit comments

Comments
 (0)