Skip to content

Implement graceful shutdown and enhance health check endpoint - #223

Merged
mnindrazaka merged 1 commit into
feat/api-vpsfrom
claude/vps-api-deployment-phase-2-6cy5pr
Jul 29, 2026
Merged

Implement graceful shutdown and enhance health check endpoint#223
mnindrazaka merged 1 commit into
feat/api-vpsfrom
claude/vps-api-deployment-phase-2-6cy5pr

Conversation

@mnindrazaka

Copy link
Copy Markdown
Collaborator

Summary

This PR improves the API server's reliability and observability by implementing graceful shutdown handling and enhancing the health check endpoint with detailed server information.

Key Changes

  • Graceful Shutdown: Replaced simple http.ListenAndServe() with a properly configured http.Server that handles OS signals (SIGINT, SIGTERM) and drains in-flight requests with a 30-second timeout before shutdown
  • Enhanced Health Check: Refactored the inline health check handler into a dedicated HealthHandler struct that returns JSON with server status, version, commit hash, and uptime
  • Server Configuration: Added configurable timeouts for read/write operations and idle connections to improve resource management
  • Bind Address Configuration: Added BIND_ADDR environment variable to allow binding to specific network interfaces (useful for production deployments behind reverse proxies)
  • Comprehensive Testing: Added unit tests for the new health check handler

Implementation Details

  • The server now listens on a goroutine and uses a select statement to handle both server errors and shutdown signals
  • Health check response includes uptime_seconds calculated from server start time
  • Graceful shutdown logs informative messages at each stage (signal received, draining requests, shutdown complete)
  • Server timeouts are configured to prevent resource exhaustion:
    • ReadHeaderTimeout: 5s
    • ReadTimeout: 15s
    • WriteTimeout: 30s
    • IdleTimeout: 120s

https://claude.ai/code/session_015MeZdpNcuQKqDPttgehybp

Replace http.ListenAndServe with an http.Server so SIGTERM/SIGINT drain
in-flight requests via srv.Shutdown on a 30s budget instead of dropping
connections instantly. Add ReadHeaderTimeout/ReadTimeout/WriteTimeout/
IdleTimeout so the process behaves as a well-behaved long-lived host
citizen under systemd.

Add BIND_ADDR (default "", all interfaces) so production can bind
127.0.0.1 behind Caddy while local dev is unchanged.

Extend GET /health-check to return JSON {status,version,commit,
uptime_seconds} driven by pkg/buildinfo, which the release script's
version gate depends on.

Also fix TestRentalHandler_CheckoutRentals/success, which was missing
a GetVariantById mock expectation added by a prior change to
RentalUsecase.CheckoutRentals and was failing pre-commit for every
commit on this branch.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015MeZdpNcuQKqDPttgehybp
@mnindrazaka
mnindrazaka merged commit cdcf485 into feat/api-vps Jul 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants