Reload routes config file on SIGHUP (#412)

This commit is contained in:
Justin Barlow
2025-05-11 07:02:19 -06:00
committed by GitHub
parent a46a423b0a
commit 0af57252dc
3 changed files with 31 additions and 9 deletions
+2
View File
@@ -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 ## 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: 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:
+25 -7
View File
@@ -144,7 +144,7 @@ func main() {
server.DownScaler = server.NewDownScaler(ctx, downScalerEnabled, downScalerDelay) server.DownScaler = server.NewDownScaler(ctx, downScalerEnabled, downScalerDelay)
c := make(chan os.Signal, 1) 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 != "" { if config.Routes.Config != "" {
err := server.RoutesConfig.ReadRoutesConfig(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") logrus.WithError(err).Fatal("Unable to start metrics reporter")
} }
// wait for process-stop signal // handle signals
<-c for {
logrus.Info("Stopping. Waiting for connections to complete...") sig := <-c
signal.Stop(c) switch sig {
connector.WaitForConnections() case syscall.SIGHUP:
logrus.Info("Stopped") 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")
}
}
} }
+4 -2
View File
@@ -3,10 +3,11 @@ package server
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"time"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"time"
"io/fs" "io/fs"
"os" "os"
@@ -15,6 +16,7 @@ import (
type IRoutesConfig interface { type IRoutesConfig interface {
ReadRoutesConfig(routesConfig string) ReadRoutesConfig(routesConfig string)
ReloadRoutesConfig()
AddMapping(serverAddress string, backend string) AddMapping(serverAddress string, backend string)
DeleteMapping(serverAddress string) DeleteMapping(serverAddress string)
SetDefaultRoute(backend string) SetDefaultRoute(backend string)
@@ -56,7 +58,7 @@ func (r *routesConfigImpl) ReadRoutesConfig(routesConfig string) error {
return nil return nil
} }
func (r *routesConfigImpl) reloadRoutesConfig() error { func (r *routesConfigImpl) ReloadRoutesConfig() error {
config, readErr := r.readRoutesConfigFile() config, readErr := r.readRoutesConfigFile()
if readErr != nil { if readErr != nil {