handle SIGTERM and SIGINT

This commit is contained in:
Stefan Prodan
2018-01-05 17:57:48 +02:00
parent b482d8de95
commit 362e575880
3 changed files with 44 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
package signals
import (
"os"
"os/signal"
)
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() (stopCh <-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
}
+10
View File
@@ -0,0 +1,10 @@
// +build !windows
package signals
import (
"os"
"syscall"
)
var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}
+7
View File
@@ -0,0 +1,7 @@
package signals
import (
"os"
)
var shutdownSignals = []os.Signal{os.Interrupt}