Create buffered signals channel

This commit is contained in:
Daniel Tomcej 2021-06-18 09:43:10 -07:00 committed by GitHub
parent 5ca210fa60
commit 95e0633b2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 21 deletions

View file

@ -1,20 +0,0 @@
package cmd
import (
"context"
"os"
"os/signal"
"syscall"
)
// ContextWithSignal creates a context canceled when SIGINT or SIGTERM are notified.
func ContextWithSignal(ctx context.Context) context.Context {
newCtx, cancel := context.WithCancel(ctx)
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signals
cancel()
}()
return newCtx
}

View file

@ -6,9 +6,11 @@ import (
stdlog "log" stdlog "log"
"net/http" "net/http"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
"syscall"
"time" "time"
"github.com/coreos/go-systemd/daemon" "github.com/coreos/go-systemd/daemon"
@ -119,7 +121,7 @@ func runCmd(staticConfiguration *static.Configuration) error {
return err return err
} }
ctx := cmd.ContextWithSignal(context.Background()) ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
if staticConfiguration.Experimental != nil && staticConfiguration.Experimental.DevPlugin != nil { if staticConfiguration.Experimental != nil && staticConfiguration.Experimental.DevPlugin != nil {
var cancel context.CancelFunc var cancel context.CancelFunc