Skip to content

Commit

Permalink
Merge pull request #69 from getamis/not-expose-check-health
Browse files Browse the repository at this point in the history
health: not expose CheckHealth() method
  • Loading branch information
markya0616 authored May 20, 2019
2 parents c777fa0 + e86ca04 commit 558ecfd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion health/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *server) Liveness(ctx context.Context, req *EmptyRequest) (*EmptyRespons

// Readiness is represented that whether application is ready to start accepting traffic or not.
func (s *server) Readiness(ctx context.Context, req *EmptyRequest) (*EmptyResponse, error) {
if err := CheckHealth(ctx, s.checkFns); err != nil {
if err := checkHealth(ctx, s.checkFns); err != nil {
log.Error("Failed to check readiness", "err", err)
return nil, status.Error(codes.Unavailable, err.Error())
}
Expand Down
8 changes: 6 additions & 2 deletions health/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ func GRPCServerHealthChecker(addr, serviceName string) CheckFn {
}
}

func CheckHealth(ctx context.Context, checkFns []CheckFn) error {
func checkHealth(parentCtx context.Context, checkFns []CheckFn) error {
if len(checkFns) == 0 {
return nil
}
errCh := make(chan error, len(checkFns))

ctx, cancel := context.WithCancel(parentCtx)
defer cancel()

for _, checker := range checkFns {
go func(checker CheckFn) {
errCh <- checker(ctx)
Expand All @@ -65,7 +69,7 @@ func CheckHealth(ctx context.Context, checkFns []CheckFn) error {
// SetLivenessAndReadiness sets the liveness and readiness route in http server mux
func SetLivenessAndReadiness(mux *http.ServeMux, checkFns ...CheckFn) {
mux.HandleFunc("/readiness", func(rw http.ResponseWriter, r *http.Request) {
if err := CheckHealth(r.Context(), checkFns); err != nil {
if err := checkHealth(r.Context(), checkFns); err != nil {
log.Error("Failed to check readiness", "err", err)
rw.WriteHeader(http.StatusServiceUnavailable)
return
Expand Down

0 comments on commit 558ecfd

Please sign in to comment.