0Pricing
Go Academy · Lesson

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

  1. Creating an HTTP Server
  2. Routing and Path Parameters
  3. Middleware Pattern
  4. Graceful Shutdown
← Back to Go Academy