clean HTTP sockets on shutdown

This commit is contained in:
boyska 2018-11-10 17:36:17 +01:00
parent 917e457af0
commit c70e28ff27

View file

@ -58,6 +58,7 @@ func main() {
go hub.Run() go hub.Run()
setupHTTP(hub) setupHTTP(hub)
httpServer := http.Server{Handler: nil}
if *querySocket != "" { if *querySocket != "" {
fmt.Printf("Binding address `%s` [http]\n", *querySocket) fmt.Printf("Binding address `%s` [http]\n", *querySocket)
unixListener, err := net.Listen("unix", *querySocket) unixListener, err := net.Listen("unix", *querySocket)
@ -65,16 +66,17 @@ func main() {
fmt.Fprintln(os.Stderr, "Error binding HTTP unix domain socket", err) fmt.Fprintln(os.Stderr, "Error binding HTTP unix domain socket", err)
return return
} }
defer cleanSocket(*querySocket)
go func() { go func() {
err := http.Serve(unixListener, nil) if err := httpServer.Serve(unixListener); err != nil {
if err != nil {
fmt.Fprintln(os.Stderr, "error binding:", err) fmt.Fprintln(os.Stderr, "error binding:", err)
} }
}() }()
} else { } else {
httpServer.Addr = *queryAddr
fmt.Printf("Binding address `%s` [http]\n", *queryAddr) fmt.Printf("Binding address `%s` [http]\n", *queryAddr)
go func() { go func() {
err := http.ListenAndServe(*queryAddr, nil) err := httpServer.ListenAndServe()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "error binding:", err) fmt.Fprintln(os.Stderr, "error binding:", err)
} }
@ -85,6 +87,9 @@ func main() {
select { select {
case <-interrupt: case <-interrupt:
server.Kill() server.Kill()
if err := httpServer.Close(); err != nil {
fmt.Fprintln(os.Stderr, "Error closing http server:", err)
}
return return
default: default:
} }