feat(cmd): add -R auto-reload flag for periodic config refresh

Introduce -R <duration> flag (e.g., -R 30s, -R 1m) that triggers
periodic config reload. Combined with -C URL support, this enables
centralized fleet management where nodes poll a remote config server.

The reload goroutine reuses the existing SIGHUP reloadConfig path:
parser.Parse() → loader.Load() → p.run(cfg). When -R is zero
(default) behavior is unchanged; SIGHUP still works independently.
master
ginuerzh 2026-06-20 20:12:11 +08:00
parent 73069f50e3
commit 1178c4757f
2 changed files with 19 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import (
"runtime"
"strings"
"sync"
"time"
"github.com/go-gost/core/logger"
xlogger "github.com/go-gost/x/logger"
@ -36,6 +37,7 @@ var (
trace bool
apiAddr string
metricsAddr string
reload time.Duration
)
func init() {
@ -87,13 +89,14 @@ func init() {
flag.Var(&services, "L", "service list")
flag.Var(&nodes, "F", "chain node list")
flag.Var(&cfgFiles, "C", "configuration file(s)")
flag.Var(&cfgFiles, "C", "config file(s), URL(s), or inline JSON")
flag.BoolVar(&printVersion, "V", false, "print version")
flag.StringVar(&outputFormat, "O", "", "output format, one of yaml|json format")
flag.BoolVar(&debug, "D", false, "debug mode")
flag.BoolVar(&trace, "DD", false, "trace mode")
flag.StringVar(&apiAddr, "api", "", "api service address")
flag.StringVar(&metricsAddr, "metrics", "", "metrics service address")
flag.DurationVar(&reload, "R", 0, "auto reload period (e.g. 30s, 1m)")
flag.Parse()
if printVersion {

View File

@ -8,6 +8,7 @@ import (
"os/signal"
"strings"
"syscall"
"time"
"github.com/go-gost/core/auth"
"github.com/go-gost/core/logger"
@ -193,6 +194,13 @@ func (p *program) reload(ctx context.Context) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
var ticker <-chan time.Time
if reload > 0 {
t := time.NewTicker(reload)
defer t.Stop()
ticker = t.C
}
for {
select {
case <-c:
@ -202,6 +210,13 @@ func (p *program) reload(ctx context.Context) {
logger.Default().Info("config reloaded")
}
case <-ticker:
if err := p.reloadConfig(); err != nil {
logger.Default().Errorf("auto reload: %v", err)
} else {
logger.Default().Debug("config auto reloaded")
}
case <-ctx.Done():
return
}