Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log shutdown gracefully #428

Merged
merged 4 commits into from
Oct 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"context"
"fmt"
"net"
"net/http"
Expand All @@ -25,7 +26,6 @@ import (
"path/filepath"
"strings"
"syscall"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
Expand Down Expand Up @@ -194,13 +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)
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)
server := &http.Server{
Addr: *listenAddress,
Handler: mux,
}

go shutdownServerOnQuit(server, quitCh, logger)
err = web.Serve(l, server, *webConfig, logger)

// 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 {
level.Error(logger).Log("msg", "HTTP server stopped", "err", err)
}

if err := ms.Shutdown(); err != nil {
level.Error(logger).Log("msg", "problem shutting down metric storage", "err", err)
}
Expand Down Expand Up @@ -240,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)

Expand All @@ -254,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())
}