core: Config load interval only reloads if changed (#4603)

This commit is contained in:
Noorain Panjwani 2022-03-02 00:02:33 +05:30 committed by GitHub
parent 186fdba916
commit 7ea5b2a818
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 2 deletions

View File

@ -481,11 +481,23 @@ func finishSettingUp(ctx Context, cfg *Config) error {
return fmt.Errorf("loading config loader module: %s", err) return fmt.Errorf("loading config loader module: %s", err)
} }
runLoadedConfig := func(config []byte) { runLoadedConfig := func(config []byte) {
Log().Info("applying dynamically-loaded config", zap.String("loader_module", val.(Module).CaddyModule().ID.Name()), zap.Int("pull_interval", int(cfg.Admin.Config.LoadInterval)))
currentCfgMu.Lock() currentCfgMu.Lock()
defer currentCfgMu.Unlock()
// Skip if there is no change in the config
if bytes.Equal(rawCfgJSON, config) {
return
}
Log().Info("applying dynamically-loaded config", zap.String("loader_module", val.(Module).CaddyModule().ID.Name()), zap.Int("pull_interval", int(cfg.Admin.Config.LoadInterval)))
err := unsyncedDecodeAndRun(config, false) err := unsyncedDecodeAndRun(config, false)
currentCfgMu.Unlock()
if err == nil { if err == nil {
// success, so update our stored copy of the encoded
// config to keep it consistent with what caddy is now
// running (storing an encoded copy is not strictly
// necessary, but avoids an extra json.Marshal for
// each config change)
rawCfgJSON = config
Log().Info("dynamically-loaded config applied successfully") Log().Info("dynamically-loaded config applied successfully")
} else { } else {
Log().Error("running dynamically-loaded config failed", zap.Error(err)) Log().Error("running dynamically-loaded config failed", zap.Error(err))
@ -503,6 +515,7 @@ func finishSettingUp(ctx Context, cfg *Config) error {
Log().Error("loading dynamic config failed", zap.Error(err)) Log().Error("loading dynamic config failed", zap.Error(err))
return return
} }
runLoadedConfig(loadedConfig) runLoadedConfig(loadedConfig)
case <-ctx.Done(): case <-ctx.Done():
if !timer.Stop() { if !timer.Stop() {