From 769064a274afa7df2a90818690f8d2ffb71d03ec Mon Sep 17 00:00:00 2001 From: wanghaowei Date: Fri, 5 Jan 2024 16:46:13 +0800 Subject: [PATCH 1/7] Add graceful shutdown. --- main.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/main.go b/main.go index 6ab8424c..34c73093 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,11 @@ import ( "flag" "net/http" "os" + "os/signal" "runtime" "strconv" "strings" + "syscall" "time" "github.com/prometheus/client_golang/prometheus" @@ -224,4 +226,14 @@ func main() { } else { log.Fatal(http.ListenAndServe(*listenAddress, exp)) } + + s := make(chan os.Signal, 1) + signal.Notify(s, syscall.SIGINT, syscall.SIGTERM) + go func() { + select { + case _s := <-s: + log.Infof("Receive exit signal, bye! signal: %s", _s.String()) + os.Exit(0) + } + }() } From d7c75114ece5e5d4f27bd68a799324228ff2da3e Mon Sep 17 00:00:00 2001 From: wanghaowei Date: Fri, 5 Jan 2024 16:58:39 +0800 Subject: [PATCH 2/7] Add graceful shutdown. --- main.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 34c73093..bdf6ce3a 100644 --- a/main.go +++ b/main.go @@ -229,11 +229,7 @@ func main() { s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGINT, syscall.SIGTERM) - go func() { - select { - case _s := <-s: - log.Infof("Receive exit signal, bye! signal: %s", _s.String()) - os.Exit(0) - } - }() + _sig := <-s + log.Infof("Receive exit signal, bye! Signal: %s", _sig.String()) + os.Exit(0) } From 284f8ee0f93c78b589ae8ce376d46b412a71fe0f Mon Sep 17 00:00:00 2001 From: wanghaowei Date: Fri, 5 Jan 2024 17:17:23 +0800 Subject: [PATCH 3/7] Add graceful shutdown. --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index bdf6ce3a..947043c8 100644 --- a/main.go +++ b/main.go @@ -230,6 +230,6 @@ func main() { s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGINT, syscall.SIGTERM) _sig := <-s - log.Infof("Receive exit signal, bye! Signal: %s", _sig.String()) + log.Infof("Receive exit signal,bye! Signal: %s", _sig.String()) os.Exit(0) } From e87aa1bcd3fe78dec5b0cebbe44b39efb772be7c Mon Sep 17 00:00:00 2001 From: wanghaowei Date: Mon, 8 Jan 2024 14:50:37 +0800 Subject: [PATCH 4/7] Add graceful shutdown. --- main.go | 55 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 947043c8..a563d628 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main import ( + "context" + "errors" "flag" "net/http" "os" @@ -210,26 +212,41 @@ func main() { log.Infof("Providing metrics at %s%s", *listenAddress, *metricPath) log.Debugf("Configured redis addr: %#v", *redisAddr) - if *tlsServerCertFile != "" && *tlsServerKeyFile != "" { - log.Debugf("Bind as TLS using cert %s and key %s", *tlsServerCertFile, *tlsServerKeyFile) + server := &http.Server{ + Addr: *listenAddress, + Handler: exp, + } + go func() { + if *tlsServerCertFile != "" && *tlsServerKeyFile != "" { + log.Debugf("Bind as TLS using cert %s and key %s", *tlsServerCertFile, *tlsServerKeyFile) - tlsConfig, err := exp.CreateServerTLSConfig(*tlsServerCertFile, *tlsServerKeyFile, *tlsServerCaCertFile, *tlsServerMinVersion) - if err != nil { - log.Fatal(err) + tlsConfig, err := exp.CreateServerTLSConfig(*tlsServerCertFile, *tlsServerKeyFile, *tlsServerCaCertFile, *tlsServerMinVersion) + if err != nil { + log.Fatal(err) + } + server.TLSConfig = tlsConfig + if err := server.ListenAndServeTLS("", ""); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatalf("TLS Server error: %v", err) + } + } else { + if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatalf("Server error: %v", err) + } } - - server := &http.Server{ - Addr: *listenAddress, - TLSConfig: tlsConfig, - Handler: exp} - log.Fatal(server.ListenAndServeTLS("", "")) - } else { - log.Fatal(http.ListenAndServe(*listenAddress, exp)) + }() + + // Set up signal handling for graceful shutdown + quit := make(chan os.Signal, 1) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + _quit := <-quit + log.Infof("Receive exit signal,bye! Signal: %s", _quit.String()) + // Create a context with a timeout + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + //Shutdown the HTTP server gracefully + if err := server.Shutdown(ctx); err != nil { + log.Fatalf("Server shutdown failed: %v", err) } - - s := make(chan os.Signal, 1) - signal.Notify(s, syscall.SIGINT, syscall.SIGTERM) - _sig := <-s - log.Infof("Receive exit signal,bye! Signal: %s", _sig.String()) - os.Exit(0) + log.Infof("Server shut down gracefully") } From 3abe58f3c1c48e3a43b95c2fa666a76a5509a32f Mon Sep 17 00:00:00 2001 From: wanghaowei Date: Mon, 8 Jan 2024 15:11:47 +0800 Subject: [PATCH 5/7] Add graceful shutdown. --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index a563d628..b2dce01a 100644 --- a/main.go +++ b/main.go @@ -244,7 +244,7 @@ func main() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - //Shutdown the HTTP server gracefully + // Shutdown the HTTP server gracefully if err := server.Shutdown(ctx); err != nil { log.Fatalf("Server shutdown failed: %v", err) } From 0620b69e7a6074ab854328e0483d43da405719ca Mon Sep 17 00:00:00 2001 From: wanghaowei Date: Fri, 12 Jan 2024 10:51:31 +0800 Subject: [PATCH 6/7] Add graceful shutdown. --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index b2dce01a..f8db7818 100644 --- a/main.go +++ b/main.go @@ -235,7 +235,7 @@ func main() { } }() - // Set up signal handling for graceful shutdown + // graceful shutdown quit := make(chan os.Signal, 1) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) _quit := <-quit From ae21a0af6eb1bd1d59e1f15d328293678c931596 Mon Sep 17 00:00:00 2001 From: wanghaowei Date: Wed, 24 Jan 2024 14:08:29 +0800 Subject: [PATCH 7/7] Add graceful shutdown. --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index f8db7818..390cc54a 100644 --- a/main.go +++ b/main.go @@ -239,7 +239,7 @@ func main() { quit := make(chan os.Signal, 1) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) _quit := <-quit - log.Infof("Receive exit signal,bye! Signal: %s", _quit.String()) + log.Infof("Received %s signal, exiting", _quit.String()) // Create a context with a timeout ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel()