Skip to content

Instantly share code, notes, and snippets.

@emretanriverdi
Created April 29, 2021 06:49
Show Gist options
  • Save emretanriverdi/ac9e69d666b95e152b86a202f9b97ca6 to your computer and use it in GitHub Desktop.
Save emretanriverdi/ac9e69d666b95e152b86a202f9b97ca6 to your computer and use it in GitHub Desktop.
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