From c70e28ff270d1af15db1bee157a43d316c8e72c6 Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 10 Nov 2018 17:36:17 +0100 Subject: [PATCH] clean HTTP sockets on shutdown --- cmd/circologd/main.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cmd/circologd/main.go b/cmd/circologd/main.go index 67b248f..fa5142d 100644 --- a/cmd/circologd/main.go +++ b/cmd/circologd/main.go @@ -58,6 +58,7 @@ func main() { go hub.Run() setupHTTP(hub) + httpServer := http.Server{Handler: nil} if *querySocket != "" { fmt.Printf("Binding address `%s` [http]\n", *querySocket) unixListener, err := net.Listen("unix", *querySocket) @@ -65,16 +66,17 @@ func main() { fmt.Fprintln(os.Stderr, "Error binding HTTP unix domain socket", err) return } + defer cleanSocket(*querySocket) go func() { - err := http.Serve(unixListener, nil) - if err != nil { + if err := httpServer.Serve(unixListener); err != nil { fmt.Fprintln(os.Stderr, "error binding:", err) } }() } else { + httpServer.Addr = *queryAddr fmt.Printf("Binding address `%s` [http]\n", *queryAddr) go func() { - err := http.ListenAndServe(*queryAddr, nil) + err := httpServer.ListenAndServe() if err != nil { fmt.Fprintln(os.Stderr, "error binding:", err) } @@ -85,6 +87,9 @@ func main() { select { case <-interrupt: server.Kill() + if err := httpServer.Close(); err != nil { + fmt.Fprintln(os.Stderr, "Error closing http server:", err) + } return default: }