Skip to content

Commit

Permalink
admin, reverseproxy: Stop timers if canceled to avoid goroutine leak (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dtelyukh authored Jan 4, 2022
1 parent 249adc1 commit 2e46c2a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,16 +494,21 @@ func finishSettingUp(ctx Context, cfg *Config) error {
if cfg.Admin.Config.LoadInterval > 0 {
go func() {
for {
timer := time.NewTimer(time.Duration(cfg.Admin.Config.LoadInterval))
select {
// if LoadInterval is positive, will wait for the interval and then run with new config
case <-time.After(time.Duration(cfg.Admin.Config.LoadInterval)):
case <-timer.C:
loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
if err != nil {
Log().Error("loading dynamic config failed", zap.Error(err))
return
}
runLoadedConfig(loadedConfig)
case <-ctx.Done():
if !timer.Stop() {
// if the timer has been stopped then read from the channel
<-timer.C
}
Log().Info("stopping config load interval")
return
}
Expand Down
7 changes: 6 additions & 1 deletion modules/caddyhttp/reverseproxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,10 +792,15 @@ func (lb LoadBalancing) tryAgain(ctx caddy.Context, start time.Time, proxyErr er
}

// otherwise, wait and try the next available host
timer := time.NewTimer(time.Duration(lb.TryInterval))
select {
case <-time.After(time.Duration(lb.TryInterval)):
case <-timer.C:
return true
case <-ctx.Done():
if !timer.Stop() {
// if the timer has been stopped then read from the channel
<-timer.C
}
return false
}
}
Expand Down

0 comments on commit 2e46c2a

Please sign in to comment.