fix(config): nil panic introduced in ff934a4bb2911f5fa3c23d8fe6fea252d881fdc3; remove duplicated log

This commit is contained in:
yusing
2025-12-16 15:04:21 +08:00
parent 0a3332bd10
commit ba8d23fada
3 changed files with 4 additions and 16 deletions

View File

@@ -52,15 +52,12 @@ func logNotifyWarn(action string, err error) {
}) })
} }
var nilState *state
func Load() error { func Load() error {
if HasState() { if HasState() {
panic(errors.New("config already loaded")) panic(errors.New("config already loaded"))
} }
state := NewState() state := NewState()
config.WorkingState.Store(state) config.WorkingState.Store(state)
defer config.WorkingState.Store(nilState)
cfgWatcher = watcher.NewConfigFileWatcher(common.ConfigFileName) cfgWatcher = watcher.NewConfigFileWatcher(common.ConfigFileName)
@@ -88,11 +85,11 @@ func Reload() gperr.Error {
newState := NewState() newState := NewState()
config.WorkingState.Store(newState) config.WorkingState.Store(newState)
defer config.WorkingState.Store(nilState)
err := newState.InitFromFile(common.ConfigPath) err := newState.InitFromFile(common.ConfigPath)
if err != nil { if err != nil {
newState.Task().FinishAndWait(err) newState.Task().FinishAndWait(err)
config.WorkingState.Store(GetState())
logNotifyError("reload", err) logNotifyError("reload", err)
return gperr.New(ansi.Warning("using last config")).With(err) return gperr.New(ansi.Warning("using last config")).With(err)
} }
@@ -106,7 +103,6 @@ func Reload() gperr.Error {
SetState(newState) SetState(newState)
if err := newState.StartProviders(); err != nil { if err := newState.StartProviders(); err != nil {
gperr.LogWarn("start providers error", err)
logNotifyError("start providers", err) logNotifyError("start providers", err)
return nil // continue return nil // continue
} }
@@ -121,7 +117,7 @@ func WatchChanges() {
configEventFlushInterval, configEventFlushInterval,
OnConfigChange, OnConfigChange,
func(err gperr.Error) { func(err gperr.Error) {
gperr.LogError("config reload error", err) logNotifyError("config reload", err)
}, },
) )
eventQueue.Start(cfgWatcher.Events(t.Context())) eventQueue.Start(cfgWatcher.Events(t.Context()))

View File

@@ -36,7 +36,7 @@ type State interface {
// could be nil before first call on Load // could be nil before first call on Load
var ActiveState synk.Value[State] var ActiveState synk.Value[State]
// nil-safe while loading config, nil otherwise // working state while loading config, same as ActiveState after successful load
var WorkingState synk.Value[State] var WorkingState synk.Value[State]
var ErrConfigChanged = errors.New("config changed") var ErrConfigChanged = errors.New("config changed")

View File

@@ -74,15 +74,7 @@ func NewMonitor(r types.Route) types.HealthMonCheck {
} }
func newMonitor(u *url.URL, cfg types.HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor { func newMonitor(u *url.URL, cfg types.HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor {
state := config.WorkingState.Load() cfg.ApplyDefaults(config.WorkingState.Load().Value().Defaults.HealthCheck)
if state == nil {
state = config.ActiveState.Load()
}
if state != nil {
cfg.ApplyDefaults(state.Value().Defaults.HealthCheck)
} else {
cfg.ApplyDefaults(types.HealthCheckConfig{}) // use config defaults
}
mon := &monitor{ mon := &monitor{
config: cfg, config: cfg,
checkHealth: healthCheckFunc, checkHealth: healthCheckFunc,