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

View file

@ -1,3 +1,4 @@
//go:build windows
// +build windows // +build windows
package main package main
@ -8,8 +9,6 @@ import (
"syscall" "syscall"
) )
func makeReloadChan() <-chan os.Signal { var ReloadSignals = []os.Signal{
reload := make(chan os.Signal) syscall.SIGHUP,
signal.Notify(reload, syscall.SIGHUP)
return reload
} }