core: Config LoadInterval -> LoadDelay for clarity

And improve/clarify docs about this feature

See #4577
This commit is contained in:
Matthew Holt 2022-03-01 15:04:47 -07:00
parent 87a1f228b4
commit bc447e307f
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
2 changed files with 19 additions and 13 deletions

View file

@ -101,20 +101,26 @@ type ConfigSettings struct {
// are not persisted; only configs that are pushed to Caddy get persisted. // are not persisted; only configs that are pushed to Caddy get persisted.
Persist *bool `json:"persist,omitempty"` Persist *bool `json:"persist,omitempty"`
// Loads a configuration to use. This is helpful if your configs are // Loads a new configuration. This is helpful if your configs are
// managed elsewhere, and you want Caddy to pull its config dynamically // managed elsewhere and you want Caddy to pull its config dynamically
// when it starts. The pulled config completely replaces the current // when it starts. The pulled config completely replaces the current
// one, just like any other config load. It is an error if a pulled // one, just like any other config load. It is an error if a pulled
// config is configured to pull another config. // config is configured to pull another config without a load_delay,
// as this creates a tight loop.
// //
// EXPERIMENTAL: Subject to change. // EXPERIMENTAL: Subject to change.
LoadRaw json.RawMessage `json:"load,omitempty" caddy:"namespace=caddy.config_loaders inline_key=module"` LoadRaw json.RawMessage `json:"load,omitempty" caddy:"namespace=caddy.config_loaders inline_key=module"`
// The interval to pull config. With a non-zero value, will pull config // The duration after which to load config. If set, config will be pulled
// from config loader (eg. a http loader) with given interval. // from the config loader after this duration. A delay is required if a
// dynamically-loaded config is configured to load yet another config. To
// load configs on a regular interval, ensure this value is set the same
// on all loaded configs; it can also be variable if needed, and to stop
// the loop, simply remove dynamic config loading from the next-loaded
// config.
// //
// EXPERIMENTAL: Subject to change. // EXPERIMENTAL: Subject to change.
LoadInterval Duration `json:"load_interval,omitempty"` LoadDelay Duration `json:"load_delay,omitempty"`
} }
// IdentityConfig configures management of this server's identity. An identity // IdentityConfig configures management of this server's identity. An identity

View file

@ -268,8 +268,8 @@ func unsyncedDecodeAndRun(cfgJSON []byte, allowPersist bool) error {
newCfg.Admin != nil && newCfg.Admin != nil &&
newCfg.Admin.Config != nil && newCfg.Admin.Config != nil &&
newCfg.Admin.Config.LoadRaw != nil && newCfg.Admin.Config.LoadRaw != nil &&
newCfg.Admin.Config.LoadInterval <= 0 { newCfg.Admin.Config.LoadDelay <= 0 {
return fmt.Errorf("recursive config loading detected: pulled configs cannot pull other configs without positive load_interval") return fmt.Errorf("recursive config loading detected: pulled configs cannot pull other configs without positive load_delay")
} }
// run the new config and start all its apps // run the new config and start all its apps
@ -483,7 +483,7 @@ func finishSettingUp(ctx Context, cfg *Config) error {
logger := Log().Named("config_loader").With( logger := Log().Named("config_loader").With(
zap.String("module", val.(Module).CaddyModule().ID.Name()), zap.String("module", val.(Module).CaddyModule().ID.Name()),
zap.Int("pull_interval", int(cfg.Admin.Config.LoadInterval))) zap.Int("load_delay", int(cfg.Admin.Config.LoadDelay)))
runLoadedConfig := func(config []byte) { runLoadedConfig := func(config []byte) {
logger.Info("applying dynamically-loaded config") logger.Info("applying dynamically-loaded config")
@ -495,9 +495,9 @@ func finishSettingUp(ctx Context, cfg *Config) error {
} }
} }
if cfg.Admin.Config.LoadInterval > 0 { if cfg.Admin.Config.LoadDelay > 0 {
go func() { go func() {
timer := time.NewTimer(time.Duration(cfg.Admin.Config.LoadInterval)) timer := time.NewTimer(time.Duration(cfg.Admin.Config.LoadDelay))
select { select {
case <-timer.C: case <-timer.C:
loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx) loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
@ -510,11 +510,11 @@ func finishSettingUp(ctx Context, cfg *Config) error {
if !timer.Stop() { if !timer.Stop() {
<-timer.C <-timer.C
} }
Log().Info("stopping config load interval") Log().Info("stopping dynamic config loading")
} }
}() }()
} else { } else {
// if no LoadInterval is provided, will load config synchronously // if no LoadDelay is provided, will load config synchronously
loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx) loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
if err != nil { if err != nil {
return fmt.Errorf("loading dynamic config from %T: %v", val, err) return fmt.Errorf("loading dynamic config from %T: %v", val, err)