func mainInterruptHandler() {
	var interruptCallbacks []func()
	invokeCallbacks := func() {
		for i := range interruptCallbacks {
			idx := len(interruptCallbacks) - 1 - i
			interruptCallbacks[idx]()
		}
		close(interruptHandlersDone)
	}
	for {
		select {
		case sig := <-interruptChannel:
			log.Infof("Received signal (%s).  Shutting down...", sig)
			invokeCallbacks()
			return
		case <-shutdownRequestChannel:
			log.Info("Shutdown requested.  Shutting down...")
			invokeCallbacks()
			return
		case <-simulateInterruptChannel:
			log.Info("Received shutdown request.  Shutting down...")
			invokeCallbacks()
			return
		case handler := <-addHandlerChannel:
			interruptCallbacks = append(interruptCallbacks, handler)
		}
	}
}
mainInterruptHandler listens for SIGINT (Ctrl+C) signals on the
interruptChannel and invokes the registered interruptCallbacks accordingly.
It also listens for callback registration.  It must be run as a goroutine.
package main
import (
	"os"
	"os/signal"
)
var shutdownRequestChannel = make(chan struct{})
var interruptChannel chan os.Signal
var addHandlerChannel = make(chan func())
var interruptHandlersDone = make(chan struct{})
var simulateInterruptChannel = make(chan struct{}, 1)
var signals = []os.Signal{os.Interrupt}
func simulateInterrupt() {
	select {
	case simulateInterruptChannel <- struct{}{}:
	default:
	}
}