cmd: use os.SignalContext for signal management

This fixes a `go vet` failure for using an unbuffered channel with
os.Signal. This also simplifies platform-specific code.

GoFmt added new forward-compatible build tags.
This commit is contained in:
Jimmy Zelinskie 2022-01-02 15:26:05 -05:00
parent dc34044973
commit 129aac230a
3 changed files with 15 additions and 20 deletions

View file

@ -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",

View file

@ -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,
}

View file

@ -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,
}