From: jb <2850825+JyBP@users.noreply.github.com> Date: Tue, 24 Jul 2018 07:34:06 +0000 (+0200) Subject: attempt to close the client on signal X-Git-Tag: v1.0.0~61^2 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=5a09866db4c978bca457c6f454052774189a5996;p=btrtrc.git attempt to close the client on signal --- diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index f9067a79..abbb5521 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -8,7 +8,9 @@ import ( "net" "net/http" "os" + "os/signal" "strings" + "syscall" "time" "github.com/anacrolix/envpprof" @@ -150,6 +152,15 @@ func statsEnabled() bool { return *flags.Stats } +func exitSignalHandlers(client *torrent.Client) { + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) + for { + log.Printf("close signal received: %+v", <-c) + client.Close() + } +} + func main() { log.SetFlags(log.LstdFlags | log.Lshortfile) tagflag.Parse(&flags) @@ -183,6 +194,8 @@ func main() { log.Fatalf("error creating client: %s", err) } defer client.Close() + go exitSignalHandlers(client) + // Write status on the root path on the default HTTP muxer. This will be // bound to localhost somewhere if GOPPROF is set, thanks to the envpprof // import.