Overview
Implement graceful shutdown for safe server termination - complete in-flight requests and cleanup resources.
Implementation Details
Files:
internal/shutdown/manager.go - Shutdown manager
cmd/facilitator/main.go - Integration
Shutdown Manager
type Manager struct {
timeout time.Duration
callbacks []func(context.Context) error
mu sync.Mutex
shutdownCh chan struct{}
doneCh chan struct{}
}
func NewManager(timeout time.Duration) *Manager
func (m *Manager) RegisterCallback(fn func(context.Context) error)
func (m *Manager) Wait() // Blocks until SIGINT/SIGTERM
func (m *Manager) Shutdown() // Programmatic shutdown
Shutdown Sequence
- Receive SIGINT/SIGTERM
- Reject new requests (503 Service Unavailable)
- Health check returns 503 (load balancer removes instance)
- Wait for in-flight requests to complete
- Execute callbacks in LIFO order:
- Stop API server
- Close storage connections
- Close RPC connections
- Exit process
Request Tracking Middleware
func (s *Server) RequestTrackingMiddleware() echo.MiddlewareFunc
- Track active requests with atomic counter
- Reject new requests during shutdown
- Wait for counter to reach zero
Health Check Integration
func (s *Server) healthHandler(c echo.Context) error {
if atomic.LoadInt32(&s.shuttingDown) == 1 {
return c.JSON(503, ...)
}
return c.JSON(200, ...)
}
Acceptance Criteria
Dependencies
Branch
feature/graceful-shutdown
Overview
Implement graceful shutdown for safe server termination - complete in-flight requests and cleanup resources.
Implementation Details
Files:
internal/shutdown/manager.go- Shutdown managercmd/facilitator/main.go- IntegrationShutdown Manager
Shutdown Sequence
Request Tracking Middleware
Health Check Integration
Acceptance Criteria
Dependencies
Branch
feature/graceful-shutdown