Back to course

Graceful Shutdown: Cleaning up on Exit

Go (Golang) for Cloud-Native Microservices

Don't Just Kill the Process

When a microservice stops (e.g., during a deployment), it should finish ongoing requests before exiting.

Using os/signal:

go stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt)

<-stop // Wait for Ctrl+C

ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) server.Shutdown(ctx) // Stop receiving new requests and finish old ones