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: Retry dynamic config load if error or no-op (#4603)
Also fix ineffectual assignment (unrelated)
Matthew Holt committed
Mar 3, 2022 at 10:57 UTC
ceef70dbc5543436b04a5f70749be18582f35403
2 files changed
+28
-16
admin.go
+2
-1
@@ -262,7 +262,7 @@ func (admin *AdminConfig) newAdminHandler(addr NetworkAddress, remote bool) admi
262
263
// provisionAdminRouters provisions all the router modules
264
// in the admin.api namespace that need provisioning.
265
-func (admin AdminConfig) provisionAdminRouters(ctx Context) error {
265
+func (admin *AdminConfig) provisionAdminRouters(ctx Context) error {
266
for _, router := range admin.routers {
267
provisioner, ok := router.(Provisioner)
268
if !ok {
@@ -277,6 +277,7 @@ func (admin AdminConfig) provisionAdminRouters(ctx Context) error {
277
278
// We no longer need the routers once provisioned, allow for GC
279
admin.routers = nil
280
+
281
return nil
282
}
283
caddy.go
+26
-15
@@ -504,20 +504,29 @@ func finishSettingUp(ctx Context, cfg *Config) error {
504
505
if cfg.Admin.Config.LoadDelay > 0 {
506
go func() {
507
- timer := time.NewTimer(time.Duration(cfg.Admin.Config.LoadDelay))
508
- select {
509
- case <-timer.C:
510
- loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
511
- if err != nil {
512
- Log().Error("loading dynamic config failed", zap.Error(err))
513
- return
507
+ // the loop is here only to iterate if there is an error or a no-op config load,
508
+ // in which case we simply wait the delay and try again
509
+ for {
510
+ timer := time.NewTimer(time.Duration(cfg.Admin.Config.LoadDelay))
511
+ select {
512
+ case <-timer.C:
513
+ loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
514
+ if err != nil {
515
+ Log().Error("failed loading dynamic config; will retry", zap.Error(err))
516
+ continue
517
+ }
518
+ if loadedConfig == nil {
519
+ Log().Info("dynamically-loaded config was nil; will retry")
520
+ continue
521
+ }
522
+ runLoadedConfig(loadedConfig)
523
+ case <-ctx.Done():
524
+ if !timer.Stop() {
525
+ <-timer.C
526
+ }
527
+ Log().Info("stopping dynamic config loading")
528
}
515
- runLoadedConfig(loadedConfig)
516
- case <-ctx.Done():
517
- if !timer.Stop() {
518
- <-timer.C
519
- }
520
- Log().Info("stopping dynamic config loading")
529
+ break
530
}
531
}()
532
} else {
@@ -534,8 +543,10 @@ func finishSettingUp(ctx Context, cfg *Config) error {
543
return nil
544
}
545
537
-// ConfigLoader is a type that can load a Caddy config. The
538
-// returned config must be valid Caddy JSON.
546
+// ConfigLoader is a type that can load a Caddy config. If
547
+// the return value is non-nil, it must be valid Caddy JSON;
548
+// if nil or with non-nil error, it is considered to be a
549
+// no-op load and may be retried later.
550
type ConfigLoader interface {
551
LoadConfig(Context) ([]byte, error)
552
}