Graceful Shutdown
Handling OS signals and draining connections
Why graceful shutdown?
Calling os.Exit or simply stopping the server drops all in-flight requests. Graceful shutdown waits for active requests to complete before exiting.
http.Server.Shutdown
srv.Shutdown(ctx) stops accepting new connections, waits for active requests to finish, then returns. Pass a context with a deadline to cap the wait time.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
srv.Shutdown(ctx)All lessons in this course
- Creating an HTTP Server
- Routing and Path Parameters
- Middleware Pattern
- Graceful Shutdown