Files
uptimesebi/internal/platform/health.go
T
Sebastian UnterschützandClaude Opus 5.5 963c677e1c
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / test (pull_request) Canceled after 4m25s
ci / lint (pull_request) Canceled after 4m26s
ci / build (pull_request) Canceled after 0s
ci / image (pull_request) Canceled after 0s
fix: roll the API out without cutting off checkers
Checkers long-poll the API for up to 25 seconds, longer than a shutdown
waits for requests, so a rollout cut their polls off (GOAWAY, timeouts).
A draining API now ends long polls within about a second, empty, and the
checker's next poll reaches another instance.

The chart rolls the API and status page servers one pod at a time with
maxUnavailable 0, and a new pod serves for 5 seconds before an old one
goes, so the ingress has picked it up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-26 00:00:11 +01:00

65 lines
1.7 KiB
Go

package platform
import (
"context"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/gin-gonic/gin"
)
// Probe reports whether a dependency is usable.
type Probe func(context.Context) error
// Health separates liveness from readiness so a draining instance drops out
// of the load balancer before in-flight work is cancelled.
type Health struct {
probes map[string]Probe
draining atomic.Bool
drained chan struct{}
once sync.Once
}
// NewHealth returns a Health that is ready while all probes pass.
func NewHealth(probes map[string]Probe) *Health {
return &Health{probes: probes, drained: make(chan struct{})}
}
// Drain makes /readyz fail permanently. Call it before shutdown.
func (h *Health) Drain() {
h.draining.Store(true)
h.once.Do(func() { close(h.drained) })
}
// Draining is closed once Drain is called, so long-running requests can
// end early instead of holding up the shutdown.
func (h *Health) Draining() <-chan struct{} { return h.drained }
// Register mounts /healthz (liveness) and /readyz (readiness).
func (h *Health) Register(r gin.IRoutes) {
r.GET("/healthz", func(c *gin.Context) { c.Status(http.StatusNoContent) })
r.GET("/readyz", h.ready)
}
func (h *Health) ready(c *gin.Context) {
if h.draining.Load() {
c.JSON(http.StatusServiceUnavailable, gin.H{"status": "draining"})
return
}
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
defer cancel()
failed := gin.H{}
for name, p := range h.probes {
if err := p(ctx); err != nil {
failed[name] = err.Error()
}
}
if len(failed) > 0 {
c.JSON(http.StatusServiceUnavailable, gin.H{"status": "unready", "failed": failed})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ready"})
}