package srv import ( "context" "errors" "math/rand" "net/http" "sync" "time" ) func handleHealthCheckFunc(_ context.Context) func(w http.ResponseWriter, r *http.Request) { // Return http handle func return func(w http.ResponseWriter, r *http.Request) { var err error var healthChecksFailed bool // TODO: Insert useful health checks here // For multiple checks, perform concurrently // Consider using errors.Join() for multiple checks var hcWg sync.WaitGroup for range 5 { hcWg.Add(1) go func() { defer hcWg.Done() err = errors.Join(err, dummyHealthCheck(r.Context())) }() } hcWg.Wait() if err != nil { healthChecksFailed = true } // TODO: Friendly reminder... err = errors.New("WARNING: Unimplemented health-check") if healthChecksFailed { w.WriteHeader(http.StatusInternalServerError) } if err != nil { w.Write([]byte(err.Error())) } 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() } }