From 0af57252dc0bbb330cd29b59abd994edb6a1e6b6 Mon Sep 17 00:00:00 2001 From: Justin Barlow <7@netlobo.com> Date: Sun, 11 May 2025 07:02:19 -0600 Subject: [PATCH] Reload routes config file on SIGHUP (#412) --- README.md | 2 ++ cmd/mc-router/main.go | 32 +++++++++++++++++++++++++------- server/routes_config.go | 6 ++++-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0e39566..f4205e0 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,8 @@ The following shows a JSON file for routes config, where `default-server` can al } ``` +Sending a SIGHUP signal will cause mc-router to reload the routes config from disk. + ## Auto Scale Allow/Deny List The allow/deny list configuration allows limiting which players can scale up servers when using the `-auto-scale-up` option (`AUTO_SCALE_UP` env variable) and which players can cancel an active down scaler when using the `-auto-scale-down` option (`AUTO_SCALE_DOWN` env variable). Global allow/deny lists can be configured that apply to all backend servers, but server-specific lists can be added as well. There are a few important things to note about the configuration: diff --git a/cmd/mc-router/main.go b/cmd/mc-router/main.go index cc4cd6d..3624e62 100644 --- a/cmd/mc-router/main.go +++ b/cmd/mc-router/main.go @@ -144,7 +144,7 @@ func main() { server.DownScaler = server.NewDownScaler(ctx, downScalerEnabled, downScalerDelay) c := make(chan os.Signal, 1) - signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) + signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) if config.Routes.Config != "" { err := server.RoutesConfig.ReadRoutesConfig(config.Routes.Config) @@ -251,10 +251,28 @@ func main() { logrus.WithError(err).Fatal("Unable to start metrics reporter") } - // wait for process-stop signal - <-c - logrus.Info("Stopping. Waiting for connections to complete...") - signal.Stop(c) - connector.WaitForConnections() - logrus.Info("Stopped") + // handle signals + for { + sig := <-c + switch sig { + case syscall.SIGHUP: + if config.Routes.Config != "" { + logrus.Info("Received SIGHUP, reloading routes config...") + if err := server.RoutesConfig.ReloadRoutesConfig(); err != nil { + logrus. + WithError(err). + WithField("routesConfig", config.Routes.Config). + Error("Could not re-read the routes config file") + } + } + case syscall.SIGINT, syscall.SIGTERM: + logrus.WithField("signal", sig).Info("Stopping. Waiting for connections to complete...") + signal.Stop(c) + connector.WaitForConnections() + logrus.Info("Stopped") + return + default: + logrus.WithField("signal", sig).Warn("Received unexpected signal") + } + } } diff --git a/server/routes_config.go b/server/routes_config.go index 814a83e..ac074a5 100644 --- a/server/routes_config.go +++ b/server/routes_config.go @@ -3,10 +3,11 @@ package server import ( "context" "encoding/json" + "time" + "github.com/fsnotify/fsnotify" "github.com/pkg/errors" "github.com/sirupsen/logrus" - "time" "io/fs" "os" @@ -15,6 +16,7 @@ import ( type IRoutesConfig interface { ReadRoutesConfig(routesConfig string) + ReloadRoutesConfig() AddMapping(serverAddress string, backend string) DeleteMapping(serverAddress string) SetDefaultRoute(backend string) @@ -56,7 +58,7 @@ func (r *routesConfigImpl) ReadRoutesConfig(routesConfig string) error { return nil } -func (r *routesConfigImpl) reloadRoutesConfig() error { +func (r *routesConfigImpl) ReloadRoutesConfig() error { config, readErr := r.readRoutesConfigFile() if readErr != nil {