From 6a9749cd42ceefbd5b8f25e63b476881defed0fa Mon Sep 17 00:00:00 2001 From: glightfoot Date: Tue, 26 Jan 2021 13:29:23 -0500 Subject: [PATCH 1/2] don't return empty responses to lifecycle api requests Signed-off-by: glightfoot --- main.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 77f40d3..87a3ddb 100644 --- a/main.go +++ b/main.go @@ -477,9 +477,14 @@ func main() { `)) }) + + signals := make(chan os.Signal, 1) + signal.Notify(signals, os.Interrupt, syscall.SIGTERM) + if *enableLifecycle { mux.HandleFunc("/-/reload", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPut || r.Method == http.MethodPost { + fmt.Fprintf(w, "Requesting reload") if *mappingConfig == "" { level.Warn(logger).Log("msg", "Received lifecycle api reload but no mapping config to reload") return @@ -490,8 +495,9 @@ func main() { }) mux.HandleFunc("/-/quit", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPut || r.Method == http.MethodPost { + fmt.Fprintf(w, "Requesting termination... Goodbye!") level.Info(logger).Log("msg", "Received lifecycle api quit, exiting") - os.Exit(0) + signals <- os.Kill } }) } @@ -514,9 +520,6 @@ func main() { go serveHTTP(mux, *listenAddress, logger) - signals := make(chan os.Signal, 1) - signal.Notify(signals, os.Interrupt, syscall.SIGTERM) - go sighupConfigReloader(*mappingConfig, mapper, *cacheSize, logger, cacheOption) go exporter.Listen(events) From ee3b81b86477a910d576d91ba90430432f8eec4e Mon Sep 17 00:00:00 2001 From: glightfoot Date: Thu, 28 Jan 2021 15:22:45 -0500 Subject: [PATCH 2/2] use a quit channel and log exit messages Signed-off-by: glightfoot --- main.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 87a3ddb..e6ac3ad 100644 --- a/main.go +++ b/main.go @@ -478,8 +478,7 @@ func main() { `)) }) - signals := make(chan os.Signal, 1) - signal.Notify(signals, os.Interrupt, syscall.SIGTERM) + quitChan := make(chan struct{}, 1) if *enableLifecycle { mux.HandleFunc("/-/reload", func(w http.ResponseWriter, r *http.Request) { @@ -496,8 +495,7 @@ func main() { mux.HandleFunc("/-/quit", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPut || r.Method == http.MethodPost { fmt.Fprintf(w, "Requesting termination... Goodbye!") - level.Info(logger).Log("msg", "Received lifecycle api quit, exiting") - signals <- os.Kill + quitChan <- struct{}{} } }) } @@ -523,5 +521,14 @@ func main() { go sighupConfigReloader(*mappingConfig, mapper, *cacheSize, logger, cacheOption) go exporter.Listen(events) - <-signals + signals := make(chan os.Signal, 1) + signal.Notify(signals, os.Interrupt, syscall.SIGTERM) + + // quit if we get a message on either channel + select { + case sig := <-signals: + level.Info(logger).Log("msg", "Received os signal, exiting", "signal", sig.String()) + case <-quitChan: + level.Info(logger).Log("msg", "Received lifecycle api quit, exiting") + } }