Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean shutdown #92

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

buildGoModule {
pname = "oplogtoredis";
version = "3.8.1";
version = "3.8.2";
src = builtins.path { path = ./.; };

postInstall = ''
Expand Down
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a race condition?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small deal since the only result is the wrong log message but worth making sure the handlers go off in the right order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but like you mentioned I'm not sure it matters. The risk is that if it's shut down in the middle of an HTTP server error it won't get reported. The handlers seem to work correctly from my testing. We have three options here: 1) leave as-is, 2) include the HTTP error (messier), 3) add locks (more complexity).

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
Loading