Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 21:45 UTC
core: Revert 7f364c7; simplify dynamic config load
Fixes #4577
Matthew Holt committed
Mar 1, 2022 at 13:00 UTC
acbee9470818a32f77634b0c0943bc0544f6e736
1 file changed
+22
-37
caddy.go
+22
-37
@@ -148,7 +148,7 @@ func changeConfig(method, path string, input []byte, forceReload bool) error {
148
149
// if nothing changed, no need to do a whole reload unless the client forces it
150
if !forceReload && bytes.Equal(rawCfgJSON, newCfg) {
151
- Log().Named("admin.api").Info("config is unchanged")
151
+ Log().Info("config is unchanged")
152
return nil
153
}
154
@@ -480,51 +480,37 @@ func finishSettingUp(ctx Context, cfg *Config) error {
480
if err != nil {
481
return fmt.Errorf("loading config loader module: %s", err)
482
}
483
- runLoadedConfig := func(config []byte) {
484
- currentCfgMu.Lock()
485
- defer currentCfgMu.Unlock()
483
487
- // Skip if there is no change in the config
488
- if bytes.Equal(rawCfgJSON, config) {
489
- return
490
- }
484
+ logger := Log().Named("config_loader").With(
485
+ zap.String("module", val.(Module).CaddyModule().ID.Name()),
486
+ zap.Int("pull_interval", int(cfg.Admin.Config.LoadInterval)))
487
492
- Log().Info("applying dynamically-loaded config", zap.String("loader_module", val.(Module).CaddyModule().ID.Name()), zap.Int("pull_interval", int(cfg.Admin.Config.LoadInterval)))
493
- err := unsyncedDecodeAndRun(config, false)
488
+ runLoadedConfig := func(config []byte) {
489
+ logger.Info("applying dynamically-loaded config")
490
+ err := changeConfig(http.MethodPost, "/"+rawConfigKey, config, false)
491
if err == nil {
495
- // success, so update our stored copy of the encoded
496
- // config to keep it consistent with what caddy is now
497
- // running (storing an encoded copy is not strictly
498
- // necessary, but avoids an extra json.Marshal for
499
- // each config change)
500
- rawCfgJSON = config
501
- Log().Info("dynamically-loaded config applied successfully")
492
+ logger.Info("successfully applied dynamically-loaded config")
493
} else {
503
- Log().Error("running dynamically-loaded config failed", zap.Error(err))
494
+ logger.Error("failed to run dynamically-loaded config", zap.Error(err))
495
}
496
}
497
+
498
if cfg.Admin.Config.LoadInterval > 0 {
499
go func() {
508
- for {
509
- timer := time.NewTimer(time.Duration(cfg.Admin.Config.LoadInterval))
510
- select {
511
- // if LoadInterval is positive, will wait for the interval and then run with new config
512
- case <-timer.C:
513
- loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
514
- if err != nil {
515
- Log().Error("loading dynamic config failed", zap.Error(err))
516
- return
517
- }
518
-
519
- runLoadedConfig(loadedConfig)
520
- case <-ctx.Done():
521
- if !timer.Stop() {
522
- // if the timer has been stopped then read from the channel
523
- <-timer.C
524
- }
525
- Log().Info("stopping config load interval")
500
+ timer := time.NewTimer(time.Duration(cfg.Admin.Config.LoadInterval))
501
+ select {
502
+ case <-timer.C:
503
+ loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
504
+ if err != nil {
505
+ Log().Error("loading dynamic config failed", zap.Error(err))
506
return
507
}
508
+ runLoadedConfig(loadedConfig)
509
+ case <-ctx.Done():
510
+ if !timer.Stop() {
511
+ <-timer.C
512
+ }
513
+ Log().Info("stopping config load interval")
514
}
515
}()
516
} else {
@@ -536,7 +522,6 @@ func finishSettingUp(ctx Context, cfg *Config) error {
522
// do this in a goroutine so current config can finish being loaded; otherwise deadlock
523
go runLoadedConfig(loadedConfig)
524
}
539
-
525
}
526
527
return nil