Skip to content

Commit

Permalink
clean shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
eparker-tulip committed Dec 16, 2024
1 parent a74d8ba commit 7c7ea85
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strconv"
"strings"
"sync"
"syscall"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
Expand Down Expand Up @@ -179,11 +180,15 @@ func main() {
}(i)
}

var shuttingDown bool

// Start one more goroutine for the HTTP server
httpServer := makeHTTPServer(aggregatedRedisClients, aggregatedMongoSessions, denylist, syncer)
go func() {
httpErr := httpServer.ListenAndServe()
if httpErr != nil {
if shuttingDown {
log.Log.Warnf("HTTP Server shutdown due to signal")
} else if httpErr != nil {
panic("Could not start up HTTP server: " + httpErr.Error())
}
}()
Expand All @@ -194,11 +199,12 @@ func main() {
// if we're not ready to receive when the signal is sent.
// See examples from https://golang.org/pkg/os/signal/#Notify
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

sig := <-signalChan
shuttingDown = true

// We got a SIGINT, cleanly stop background goroutines and then return so
// We got a SIGINT or SIGTERM, cleanly stop background goroutines and then return so
// that the `defer`s above can close the Mongo and Redis connection.
//
// We also call signal.Reset() to clear our signal handler so if we get
Expand Down

0 comments on commit 7c7ea85

Please sign in to comment.