Skip to content

Commit

Permalink
fix(main): gracefully shutdown http servers
Browse files Browse the repository at this point in the history
  • Loading branch information
mchrome committed Feb 19, 2024
1 parent fead38d commit 8d26c1a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/carbonapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"net/http"
"net/http/pprof"
"os"
"os/signal"
"sync"
"syscall"
"time"

"github.com/gorilla/handlers"
"github.com/lomik/zapwriter"
Expand Down Expand Up @@ -105,13 +108,17 @@ func main() {
zap.Error(err),
)
}

servers := make([]*http.Server, 0)

for _, ip := range ips {
address := (&net.TCPAddr{IP: ip, Port: port}).String()
s := &http.Server{
Addr: address,
Handler: handler,
ErrorLog: httpLogger,
}
servers = append(servers, s)
isTLS := false
if len(listen.ServerTLSConfig.CACertFiles) > 0 {
tlsConfig, warns, err := tlsconfig.ParseServerTLSConfig(&listen.ServerTLSConfig, &listen.ClientTLSConfig)
Expand Down Expand Up @@ -152,6 +159,20 @@ func main() {
wg.Done()
}(listener, isTLS)
}

go func() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
<-stop
logger.Info("stoping carbonapi")
// initiating the shutdown
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
for _, s := range servers {
s.Shutdown(ctx)
}
cancel()
}()

}

if config.Config.Expvar.Enabled {
Expand Down

0 comments on commit 8d26c1a

Please sign in to comment.