Created
February 18, 2020 22:54
-
-
Save xeoncross/312590d8562963bb0a7152de584d4f37 to your computer and use it in GitHub Desktop.
Example of using os.Signal interrupts (or CTRL+C) to stop context which can be used to end a process, database query, or http server.
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 InterruptContext() context.Context { | |
quit := make(chan os.Signal, 1) | |
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | |
ctx, cancel := context.WithCancel(context.Background()) | |
go func() { | |
<-quit | |
cancel() | |
}() | |
return ctx | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment