Graceful shutdown: draining HTTP + background workers

7135
0

A clean shutdown is part of reliability, not an afterthought. The pattern I like is: (1) start servers and workers, (2) listen for SIGINT/SIGTERM, (3) call Shutdown with a deadline, and (4) wait for background goroutines to finish. http.Server.Shutdown stops accepting new connections and drains in-flight requests, but only if your handlers respect context cancellation. I also prefer a single context.WithCancel that gets triggered by the signal, and then each subsystem gets a derived timeout. This avoids hung deployments where pods never terminate, and it prevents partial work from being abandoned without a chance to finish or roll back.