Implementing Graceful Shutdown in Go HTTP Servers to Avoid Dropped Requests
Killing an HTTP server with SIGKILL terminates in-flight requests instantly, which can cause partial database writes, broken responses, and leaked resources. The article explains that tools like Kubernetes, systemd, and Docker send SIGTERM first, giving servers a window to exit cleanly before escalation. Go's net/http package provides Server.Shutdown(ctx), which stops new connections and waits for active handlers, but developers must wire up the signal handling themselves. The author includes a sample program that listens for SIGINT and SIGTERM and drains connections within a 30-second timeout. It also warns that Shutdown does not wait for goroutines spawned by handlers, which may need a sync.WaitGroup to avoid running after resources close.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in