-
-
Save emretanriverdi/ac9e69d666b95e152b86a202f9b97ca6 to your computer and use it in GitHub Desktop.
graceful-shutdown.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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