From e3228e8322d760a7aad832bce70b7e11a3b2536c Mon Sep 17 00:00:00 2001 From: Carl Jackson Date: Wed, 25 Jun 2014 18:20:37 -0700 Subject: [PATCH] Change graceful to opt-in to signal handling Previously, a set of standard signals would be handled automatically via an init() function, however that made the package difficult to use in packages in which an HTTP server would only be spawned some of the times (perhaps keyed on an environment variable or flag). Now, signals must be registered manually. By default, the top-level "goji" package automatically registers signals with graceful, so this will result in no behavior changes for most people. Fixes #35. --- goji.go | 1 + graceful/einhorn.go | 5 +---- graceful/signal.go | 20 ++++++++++---------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/goji.go b/goji.go index 80d3ce9..b68b93f 100644 --- a/goji.go +++ b/goji.go @@ -59,6 +59,7 @@ func Serve() { listener := bind.Default() log.Println("Starting Goji on", listener.Addr()) + graceful.HandleSignals() bind.Ready() err := graceful.Serve(listener, http.DefaultServeMux) diff --git a/graceful/einhorn.go b/graceful/einhorn.go index 8803ad9..082d1c4 100644 --- a/graceful/einhorn.go +++ b/graceful/einhorn.go @@ -3,7 +3,6 @@ package graceful import ( - "log" "os" "strconv" "syscall" @@ -18,7 +17,5 @@ func init() { if err != nil || mpid != os.Getppid() { return } - - log.Print("graceful: Einhorn detected, adding SIGUSR2 handler") - AddSignal(syscall.SIGUSR2) + stdSignals = append(stdSignals, syscall.SIGUSR2) } diff --git a/graceful/signal.go b/graceful/signal.go index c231a80..5c2ab15 100644 --- a/graceful/signal.go +++ b/graceful/signal.go @@ -24,28 +24,28 @@ var hookLock sync.Mutex var prehooks = make([]func(), 0) var posthooks = make([]func(), 0) +var stdSignals = []os.Signal{os.Interrupt} var sigchan = make(chan os.Signal, 1) func init() { - AddSignal(os.Interrupt) go waitForSignal() } +// HandleSignals installs signal handlers for a set of standard signals. By +// default, this set only includes keyboard interrupts, however when the package +// detects that it is running under Einhorn, a SIGUSR2 handler is installed as +// well. +func HandleSignals() { + AddSignal(stdSignals...) +} + // AddSignal adds the given signal to the set of signals that trigger a graceful -// shutdown. Note that for convenience the default interrupt (SIGINT) handler is -// installed at package load time, and unless you call ResetSignals() will be -// listened for in addition to any signals you provide by calling this function. +// shutdown. func AddSignal(sig ...os.Signal) { signal.Notify(sigchan, sig...) } // ResetSignals resets the list of signals that trigger a graceful shutdown. -// Useful if, for instance, you don't want to use the default interrupt (SIGINT) -// handler. Since we necessarily install the SIGINT handler before you have a -// chance to call ResetSignals(), there will be a brief window during which the -// set of signals this package listens for will not be as you intend. Therefore, -// if you intend on using this function, we encourage you to call it as soon as -// possible. func ResetSignals() { signal.Stop(sigchan) }