@marcel91200 / caddy Mirror · read-only / commits / 2e46c2ac
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 00:15 UTC

admin, reverseproxy: Stop timers if canceled to avoid goroutine leak (#4482)

Денис Телюх committed Jan 5, 2022 at 02:14 UTC 2e46c2ac1d860230a80de5fd7bc85c8c9429d3f4
2 files changed +12 -2
caddy.go
+6 -1
@@ -494,9 +494,10 @@ func finishSettingUp(ctx Context, cfg *Config) error {
494 if cfg.Admin.Config.LoadInterval > 0 {
495 go func() {
496 for {
497 + timer := time.NewTimer(time.Duration(cfg.Admin.Config.LoadInterval))
498 select {
499 // if LoadInterval is positive, will wait for the interval and then run with new config
499 - case <-time.After(time.Duration(cfg.Admin.Config.LoadInterval)):
500 + case <-timer.C:
501 loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
502 if err != nil {
503 Log().Error("loading dynamic config failed", zap.Error(err))
@@ -504,6 +505,10 @@ func finishSettingUp(ctx Context, cfg *Config) error {
505 }
506 runLoadedConfig(loadedConfig)
507 case <-ctx.Done():
508 + if !timer.Stop() {
509 + // if the timer has been stopped then read from the channel
510 + <-timer.C
511 + }
512 Log().Info("stopping config load interval")
513 return
514 }
modules/caddyhttp/reverseproxy/reverseproxy.go
+6 -1
@@ -792,10 +792,15 @@ func (lb LoadBalancing) tryAgain(ctx caddy.Context, start time.Time, proxyErr er
792 }
793
794 // otherwise, wait and try the next available host
795 + timer := time.NewTimer(time.Duration(lb.TryInterval))
796 select {
796 - case <-time.After(time.Duration(lb.TryInterval)):
797 + case <-timer.C:
798 return true
799 case <-ctx.Done():
800 + if !timer.Stop() {
801 + // if the timer has been stopped then read from the channel
802 + <-timer.C
803 + }
804 return false
805 }
806 }