Unify termination signal handling across platforms

- All supported platforms can handle the same set of signals we're
  interested in, thus we don't need build contraints to use a set of
  signals on Linux, while restricting ourselves to only SIGINT on Darwin
  and Windows.
- According to the documentation of os/signal, similar to SIGINT and
  SIGTERM, SIGHUP causes the program to exit, therefore add it to the
  list of handled signals.

Signed-off-by: Rodolfo Carvalho <rcarvalh@redhat.com>
This commit is contained in:
Rodolfo Carvalho
2016-04-05 12:48:08 +02:00
parent 2347d0f047
commit 9576f71b9a
3 changed files with 6 additions and 54 deletions

View File

@@ -20,8 +20,13 @@ import (
"os"
"os/signal"
"sync"
"syscall"
)
// terminationSignals are signals that cause the program to exit in the
// supported platforms (linux, darwin, windows).
var terminationSignals = []os.Signal{syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT}
// Handler guarantees execution of notifications after a critical section (the function passed
// to a Run method), even in the presence of process termination. It guarantees exactly once
// invocation of the provided notify functions.
@@ -82,7 +87,7 @@ func (h *Handler) Signal(s os.Signal) {
// per Handler instance, so calling Run more than once will not behave as the user expects.
func (h *Handler) Run(fn func() error) error {
ch := make(chan os.Signal, 1)
signal.Notify(ch, childSignals...)
signal.Notify(ch, terminationSignals...)
defer func() {
signal.Stop(ch)
close(ch)