Skip to content

Instantly share code, notes, and snippets.

@emretanriverdi
Created April 29, 2021 06:49
graceful-shutdown.go
func main() {
gracefulShutdown := make(chan os.Signal, 1)
signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM)
// do stuff
// ...
<-gracefulShutdown
_, cancel := context.WithTimeout(context.Background(), 3*time.Second)
// we don't need the context variable here so let's just put underscore
defer handleTermination(cancel)
fmt.Println("Done!")
}
func handleTermination(cancel context.CancelFunc) {
sendTerminationMessage() // our (dummy) method for termination message
cancel()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment