Add readiness endpoint for graceful shutdown coordination

Implement readiness endpoint (/api/ready) that returns:
- {"ready":true} (HTTP 200) during normal operation
- {"ready":false} (HTTP 503) during graceful shutdown

Key changes:
- Added readiness context to control readiness state
- Modified server.NewServer() to accept readiness context
- Implemented handleReadiness() with context-aware logic
- Updated cmd/server/main.go to manage readiness state
- Readiness set to false when shutdown signal received
- Updated test script to validate readiness behavior
- Added comprehensive documentation for readiness endpoint

This allows Kubernetes/service meshes to stop routing traffic
to the pod during graceful shutdown while allowing existing
requests to complete. Health endpoint continues to return
happy status during shutdown for proper orchestration.
This commit is contained in:
Gabriel Radureau
2026-04-03 19:53:14 +02:00
parent 7c5e61c386
commit f986711974
5 changed files with 123 additions and 36 deletions

View File

@@ -61,8 +61,12 @@ func main() {
// Create ongoing context for active requests
ongoingCtx, stopOngoingGracefully := context.WithCancel(context.Background())
// Create readiness context to control readiness state
readyCtx, readyCancel := context.WithCancel(context.Background())
defer readyCancel()
// Start server in goroutine
server := server.NewServer(cfg)
server := server.NewServer(cfg, readyCtx)
serverCtx, serverStop := context.WithCancel(ctx)
go func() {
@@ -87,7 +91,11 @@ func main() {
<-rootCtx.Done()
stop()
log.Info().Msg("Shutdown signal received")
// Cancel readiness context to stop accepting new requests
readyCancel()
log.Info().Msg("Readiness set to false, no longer accepting new requests")
// Give time for readiness check to propagate (simplified for our case)
time.Sleep(1 * time.Second)
log.Info().Msg("Readiness check propagated, now waiting for ongoing requests to finish.")