Skip to content

Commit

Permalink
Allow setting server-wide status via StaticChecker (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhump authored Jan 23, 2025
1 parent 9edeb98 commit 83ff7f6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions grpchealth.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ func NewStaticChecker(services ...string) *StaticChecker {

// SetStatus sets the health status of a service, registering a new service if
// necessary. It's safe to call SetStatus and Check concurrently.
//
// If the given service name is empty, it sets a server-wide status that is
// returned to check requests that do not request a particular service. If no
// such status is ever set, checks that do not request a particular service
// will get a response of StatusServing.
func (c *StaticChecker) SetStatus(service string, status Status) {
c.mu.Lock()
defer c.mu.Unlock()
Expand All @@ -184,14 +189,14 @@ func (c *StaticChecker) SetStatus(service string, status Status) {

// Check implements Checker. It's safe to call concurrently with SetStatus.
func (c *StaticChecker) Check(_ context.Context, req *CheckRequest) (*CheckResponse, error) {
if req.Service == "" {
return &CheckResponse{Status: StatusServing}, nil
}
c.mu.RLock()
defer c.mu.RUnlock()
if status, registered := c.statuses[req.Service]; registered {
return &CheckResponse{Status: status}, nil
}
if req.Service == "" {
return &CheckResponse{Status: StatusServing}, nil
}
return nil, connect.NewError(
connect.CodeNotFound,
fmt.Errorf("unknown service %s", req.Service),
Expand Down
4 changes: 4 additions & 0 deletions grpchealth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func TestHealth(t *testing.T) {
}

assertStatus(t, "" /* process */, StatusServing)
checker.SetStatus("", StatusNotServing)
assertStatus(t, "", StatusNotServing)
checker.SetStatus("", StatusServing)
assertStatus(t, "", StatusServing)

assertStatus(t, userFQN, StatusServing)
checker.SetStatus(userFQN, StatusNotServing)
Expand Down

0 comments on commit 83ff7f6

Please sign in to comment.