diff --git a/cmd/chihaya/main.go b/cmd/chihaya/main.go index f797bc4..1373f63 100644 --- a/cmd/chihaya/main.go +++ b/cmd/chihaya/main.go @@ -1,8 +1,8 @@ package main import ( + "context" "errors" - "os" "os/signal" "runtime" "strings" @@ -144,15 +144,13 @@ func RootRunCmdFunc(cmd *cobra.Command, args []string) error { return err } - quit := make(chan os.Signal) - signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) - - reload := makeReloadChan() + ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + reload, _ := signal.NotifyContext(context.Background(), ReloadSignals...) for { select { - case <-reload: - log.Info("reloading; received SIGUSR1") + case <-reload.Done(): + log.Info("reloading; received reload signal") peerStore, err := r.Stop(true) if err != nil { return err @@ -161,8 +159,8 @@ func RootRunCmdFunc(cmd *cobra.Command, args []string) error { if err := r.Start(peerStore); err != nil { return err } - case <-quit: - log.Info("shutting down; received SIGINT/SIGTERM") + case <-ctx.Done(): + log.Info("shutting down; received shutdown signal") if _, err := r.Stop(false); err != nil { return err } @@ -210,7 +208,7 @@ func RootPostRunCmdFunc(cmd *cobra.Command, args []string) error { } func main() { - var rootCmd = &cobra.Command{ + rootCmd := &cobra.Command{ Use: "chihaya", Short: "BitTorrent Tracker", Long: "A customizable, multi-protocol BitTorrent Tracker", @@ -229,7 +227,7 @@ func main() { rootCmd.Flags().String("config", "/etc/chihaya.yaml", "location of configuration file") - var e2eCmd = &cobra.Command{ + e2eCmd := &cobra.Command{ Use: "e2e", Short: "exec e2e tests", Long: "Execute the Chihaya end-to-end test suite", diff --git a/cmd/chihaya/signal_unix.go b/cmd/chihaya/signal_unix.go index e2c6987..aaa9201 100644 --- a/cmd/chihaya/signal_unix.go +++ b/cmd/chihaya/signal_unix.go @@ -1,15 +1,13 @@ +//go:build darwin || freebsd || linux || netbsd || openbsd || dragonfly || solaris // +build darwin freebsd linux netbsd openbsd dragonfly solaris package main import ( "os" - "os/signal" "syscall" ) -func makeReloadChan() <-chan os.Signal { - reload := make(chan os.Signal) - signal.Notify(reload, syscall.SIGUSR1) - return reload +var ReloadSignals = []os.Signal{ + syscall.SIGUSR1, } diff --git a/cmd/chihaya/signal_windows.go b/cmd/chihaya/signal_windows.go index ebd0d43..603e8a9 100644 --- a/cmd/chihaya/signal_windows.go +++ b/cmd/chihaya/signal_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package main @@ -8,8 +9,6 @@ import ( "syscall" ) -func makeReloadChan() <-chan os.Signal { - reload := make(chan os.Signal) - signal.Notify(reload, syscall.SIGHUP) - return reload +var ReloadSignals = []os.Signal{ + syscall.SIGHUP, }