Skip to content

Instantly share code, notes, and snippets.

@luohua13
Created August 30, 2019 03:12
Show Gist options
  • Save luohua13/7652fa6c4bb5ba0f20b63f506215fb42 to your computer and use it in GitHub Desktop.
Save luohua13/7652fa6c4bb5ba0f20b63f506215fb42 to your computer and use it in GitHub Desktop.
var onlyOneSignalHandler = make(chan struct{})
// SetupSignalHandler registered for SIGTERM and SIGINT. A stop channel is returned
// which is closed on one of these signals. If a second signal is caught, the program
// is terminated with exit code 1.
func SetupSignalHandler() <-chan struct{} {
close(onlyOneSignalHandler) // panics when called twice
stop := make(chan struct{})
c := make(chan os.Signal, 2)
signal.Notify(c, shutdownSignals...)
go func() {
<-c
close(stop)
<-c
os.Exit(1) // second signal. Exit directly.
}()
return stop
}
command := app.NewAPIServerCommand(server.SetupSignalHandler())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment