Skip to content

Commit

Permalink
Add graceful shutdown (#867)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghaowei0107 authored Jan 25, 2024
1 parent 809380b commit 27e114b
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package main

import (
"context"
"errors"
"flag"
"net/http"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -208,20 +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))
}()

// graceful shutdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
_quit := <-quit
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()

// Shutdown the HTTP server gracefully
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Server shutdown failed: %v", err)
}
log.Infof("Server shut down gracefully")
}

0 comments on commit 27e114b

Please sign in to comment.