From 124ed2238442404a35ac0ba2f420b6020638ba64 Mon Sep 17 00:00:00 2001 From: Geoff Bourne Date: Tue, 21 Oct 2025 19:10:58 -0500 Subject: [PATCH 1/2] Use signal.NotifyContext and WaitGroup.Go --- cmd/mc-router/main.go | 29 ++++++++++++++++------------- go.mod | 2 +- server/server.go | 16 +++------------- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/cmd/mc-router/main.go b/cmd/mc-router/main.go index 747c077..01ea2e4 100644 --- a/cmd/mc-router/main.go +++ b/cmd/mc-router/main.go @@ -3,12 +3,14 @@ package main import ( "context" "fmt" - "github.com/itzg/go-flagsfiller" - "github.com/itzg/mc-router/server" - "github.com/sirupsen/logrus" "os" "os/signal" + "sync" "syscall" + + "github.com/itzg/go-flagsfiller" + "github.com/itzg/mc-router/server" + "github.com/sirupsen/logrus" ) var ( @@ -49,36 +51,37 @@ func main() { logrus.Debug("Debug logs enabled") } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() signals := make(chan os.Signal, 1) - signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) + signal.Notify(signals, syscall.SIGHUP) s, err := server.NewServer(ctx, &cliConfig.ServerConfig) if err != nil { logrus.WithError(err).Fatal("Could not setup server") } - go s.Run() + var wg sync.WaitGroup + wg.Go(s.Run) +signalsLoop: for { select { - case <-s.Done(): - return + case <-ctx.Done(): + break signalsLoop case sig := <-signals: switch sig { case syscall.SIGHUP: s.ReloadConfig() - case syscall.SIGINT, syscall.SIGTERM: - cancel() - // but wait for the server to be done - default: logrus.WithField("signal", sig).Warn("Received unexpected signal") } } } + + logrus.Info("Stopping") + wg.Wait() } diff --git a/go.mod b/go.mod index 542a5d7..898fef2 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/itzg/mc-router -go 1.24.4 +go 1.25 require ( github.com/fsnotify/fsnotify v1.9.0 diff --git a/server/server.go b/server/server.go index e5f36da..8a336fc 100644 --- a/server/server.go +++ b/server/server.go @@ -3,12 +3,13 @@ package server import ( "context" "fmt" - "github.com/sirupsen/logrus" "net" "os" "runtime/pprof" "strconv" "time" + + "github.com/sirupsen/logrus" ) type Server struct { @@ -179,15 +180,6 @@ func NewServer(ctx context.Context, config *Config) (*Server, error) { }, nil } -// Done provides a channel notified when the server has closed all connections, etc -func (s *Server) Done() <-chan struct{} { - return s.doneChan -} - -func (s *Server) notifyDone() { - s.doneChan <- struct{}{} -} - // ReloadConfig indicates that an external request, such as a SIGHUP, // is requesting the routes config file to be reloaded, if enabled func (s *Server) ReloadConfig() { @@ -209,7 +201,6 @@ func (s *Server) Run() { ) if err != nil { logrus.WithError(err).Error("Could not start accepting connections") - s.notifyDone() return } @@ -222,10 +213,9 @@ func (s *Server) Run() { } case <-s.ctx.Done(): - logrus.Info("Stopping. Waiting for connections to complete...") + logrus.Info("Server Stopping. Waiting for connections to complete...") s.connector.WaitForConnections() logrus.Info("Stopped") - s.notifyDone() return } } From bcb2c752e799d2f918257e97d1729083997083b5 Mon Sep 17 00:00:00 2001 From: Geoff Bourne Date: Tue, 21 Oct 2025 19:12:48 -0500 Subject: [PATCH 2/2] Switch to main --- .github/workflows/test.yml | 4 ++-- README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3606806..cfb15a6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,10 +3,10 @@ name: Test on: push: branches: - - master + - main pull_request: branches: - - master + - main jobs: build: diff --git a/README.md b/README.md index 22af4b1..0a021f4 100644 --- a/README.md +++ b/README.md @@ -312,7 +312,7 @@ For the port it will look in `spec.ports` for a port named `mc-router`, if not p `"mc-router.itzg.me/externalServerName"` annotation that declares their external server name(s) ```bash -kubectl apply -f https://raw.githubusercontent.com/itzg/mc-router/master/docs/k8s-example-auto.yaml +kubectl apply -f https://raw.githubusercontent.com/itzg/mc-router/main/docs/k8s-example-auto.yaml ``` ![](docs/example-deployment-auto.drawio.png)