From 25de04b5950eee24a95da8d0233de3d123192e64 Mon Sep 17 00:00:00 2001 From: mjip Date: Sun, 3 Oct 2021 14:15:20 -0400 Subject: [PATCH 1/4] Log shutdown gracefully Signed-off-by: mjip --- main.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 47b46c35..1573e7a4 100644 --- a/main.go +++ b/main.go @@ -196,7 +196,11 @@ func main() { go closeListenerOnQuit(l, quitCh, logger) err = web.Serve(l, &http.Server{Addr: *listenAddress, Handler: mux}, *webConfig, logger) - level.Error(logger).Log("msg", "HTTP server stopped", "err", err) + if err != nil { + level.Error(logger).Log("msg", "HTTP server stopped", "err", err) + } else { + level.Info(logger).Log("msg", "HTTP server stopped") + } // To give running connections a chance to submit their payload, we wait // for 1sec, but we don't want to wait long (e.g. until all connections // are done) to not delay the shutdown. From f55c21addc0f2c44d79a269ceea2155840b91f43 Mon Sep 17 00:00:00 2001 From: mjip Date: Mon, 4 Oct 2021 20:20:12 -0400 Subject: [PATCH 2/4] Adjusted error condition, added context for change Signed-off-by: mjip --- main.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 1573e7a4..02146932 100644 --- a/main.go +++ b/main.go @@ -196,11 +196,14 @@ func main() { go closeListenerOnQuit(l, quitCh, logger) err = web.Serve(l, &http.Server{Addr: *listenAddress, Handler: mux}, *webConfig, logger) - if err != nil { - level.Error(logger).Log("msg", "HTTP server stopped", "err", err) - } else { + + // In the case of a shutdown, log gracefully + if strings.Contains(err.Error(), "use of closed network connection") { level.Info(logger).Log("msg", "HTTP server stopped") + } else { + level.Error(logger).Log("msg", "HTTP server stopped", "err", err) } + // To give running connections a chance to submit their payload, we wait // for 1sec, but we don't want to wait long (e.g. until all connections // are done) to not delay the shutdown. From f7ff2004e90405582d4e4b6c2f68369ccb71fc45 Mon Sep 17 00:00:00 2001 From: mjip Date: Tue, 5 Oct 2021 20:29:17 -0400 Subject: [PATCH 3/4] Shut down HTTP server instead of closing listener Signed-off-by: mjip --- main.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 02146932..773d3913 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ package main import ( + "context" "fmt" "net" "net/http" @@ -25,7 +26,6 @@ import ( "path/filepath" "strings" "syscall" - "time" "github.com/go-kit/log" "github.com/go-kit/log/level" @@ -194,20 +194,21 @@ func main() { mux.Handle(apiPath+"/v1/", http.StripPrefix(apiPath+"/v1", av1)) - go closeListenerOnQuit(l, quitCh, logger) - err = web.Serve(l, &http.Server{Addr: *listenAddress, Handler: mux}, *webConfig, logger) + server := &http.Server{ + Addr: *listenAddress, + Handler: mux, + } + + go shutdownServerOnQuit(server, quitCh, logger) + err = web.Serve(l, server, *webConfig, logger) // In the case of a shutdown, log gracefully - if strings.Contains(err.Error(), "use of closed network connection") { + if err == http.ErrServerClosed { level.Info(logger).Log("msg", "HTTP server stopped") } else { level.Error(logger).Log("msg", "HTTP server stopped", "err", err) } - // To give running connections a chance to submit their payload, we wait - // for 1sec, but we don't want to wait long (e.g. until all connections - // are done) to not delay the shutdown. - time.Sleep(time.Second) if err := ms.Shutdown(); err != nil { level.Error(logger).Log("msg", "problem shutting down metric storage", "err", err) } @@ -247,9 +248,9 @@ func computeRoutePrefix(prefix string, externalURL *url.URL) string { return prefix } -// closeListenerOnQuite closes the provided listener upon closing the provided +// shutdownServerOnQuit shutdowns the provided server upon closing the provided // quitCh or upon receiving a SIGINT or SIGTERM. -func closeListenerOnQuit(l net.Listener, quitCh <-chan struct{}, logger log.Logger) { +func shutdownServerOnQuit(server *http.Server, quitCh <-chan struct{}, logger log.Logger) error { notifier := make(chan os.Signal, 1) signal.Notify(notifier, os.Interrupt, syscall.SIGTERM) @@ -261,5 +262,5 @@ func closeListenerOnQuit(l net.Listener, quitCh <-chan struct{}, logger log.Logg level.Warn(logger).Log("msg", "received termination request via web service, exiting gracefully...") break } - l.Close() + return server.Shutdown(context.Background()) } From 19adaee7047fc8fd209cff08292734f0065a37a0 Mon Sep 17 00:00:00 2001 From: mjip Date: Wed, 6 Oct 2021 21:57:17 -0400 Subject: [PATCH 4/4] Adjust comment wording Signed-off-by: mjip --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 773d3913..e69547b9 100644 --- a/main.go +++ b/main.go @@ -202,7 +202,7 @@ func main() { go shutdownServerOnQuit(server, quitCh, logger) err = web.Serve(l, server, *webConfig, logger) - // In the case of a shutdown, log gracefully + // In the case of a graceful shutdown, do not log the error. if err == http.ErrServerClosed { level.Info(logger).Log("msg", "HTTP server stopped") } else {