From f1867c1010914d8c36898c39938a9d62f8fb6bff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Fri, 19 Apr 2019 17:14:52 +0200 Subject: [PATCH] Improve shutdown sequence --- main.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 395497a..48b6509 100644 --- a/main.go +++ b/main.go @@ -191,12 +191,8 @@ func main() { server := &http.Server{Addr: listenAddr} // Run the producer - go func() { - if err := server.ListenAndServe(); err != nil && - err != http.ErrServerClosed { - log.Fatalln(err) - } - }() + errs := make(chan error, 1) + go func() { errs <- server.ListenAndServe() }() // Run the consumer ctx, ctxCancel := context.WithCancel(context.Background()) @@ -217,7 +213,12 @@ func main() { // Wait for a termination signal sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - <-sigs + + select { + case <-sigs: + case err := <-errs: + log.Println(err) + } // Stop the producer gracefully ctxSd, ctxSdCancel := context.WithTimeout(context.Background(), 5)