go-http-server-with-otel/pkg/srv/http_health.go

68 lines
1.3 KiB
Go
Raw Normal View History

2025-01-04 05:06:49 +00:00
package srv
import (
"context"
"errors"
"math/rand"
"net/http"
"sync"
"time"
"github.com/rs/zerolog"
2025-01-04 05:06:49 +00:00
)
type HealthCheckFunc func(context.Context) error
func handleHealthCheckFunc(ctx context.Context, hcFuncs ...HealthCheckFunc) func(w http.ResponseWriter, r *http.Request) {
2025-01-04 05:06:49 +00:00
// Return http handle func
return func(w http.ResponseWriter, r *http.Request) {
var (
healthChecksFailed bool
errs error
hcWg sync.WaitGroup
)
if len(hcFuncs) < 1 {
zerolog.Ctx(ctx).Warn().Msg("no health checks given responding with dummy 200")
hcFuncs = append(hcFuncs, dummyHealthCheck)
}
// Run all health check funcs concurrently
// log all errors
hcWg.Add(len(hcFuncs))
for _, hc := range hcFuncs {
2025-01-04 05:06:49 +00:00
go func() {
defer hcWg.Done()
errs = errors.Join(errs, hc(ctx))
2025-01-04 05:06:49 +00:00
}()
}
hcWg.Wait()
if errs != nil {
2025-01-04 05:06:49 +00:00
healthChecksFailed = true
}
if healthChecksFailed {
w.WriteHeader(http.StatusInternalServerError)
}
if errs != nil {
w.Write([]byte(errs.Error()))
2025-01-04 05:06:49 +00:00
} else {
w.Write([]byte("ok"))
}
}
}
func dummyHealthCheck(ctx context.Context) error {
workFor := rand.Intn(750)
ticker := time.NewTicker(time.Duration(time.Duration(workFor) * time.Millisecond))
select {
case <-ticker.C:
return nil
case <-ctx.Done():
return ctx.Err()
}
}