Browse Source
admin, reverseproxy: Stop timers if canceled to avoid goroutine leak (#4482)
master
Денис Телюх
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
12 additions and
2 deletions
-
caddy.go
-
modules/caddyhttp/reverseproxy/reverseproxy.go
|
@ -494,9 +494,10 @@ func finishSettingUp(ctx Context, cfg *Config) error { |
|
|
if cfg.Admin.Config.LoadInterval > 0 { |
|
|
if cfg.Admin.Config.LoadInterval > 0 { |
|
|
go func() { |
|
|
go func() { |
|
|
for { |
|
|
for { |
|
|
|
|
|
timer := time.NewTimer(time.Duration(cfg.Admin.Config.LoadInterval)) |
|
|
select { |
|
|
select { |
|
|
// if LoadInterval is positive, will wait for the interval and then run with new config
|
|
|
// 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) |
|
|
loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
Log().Error("loading dynamic config failed", zap.Error(err)) |
|
|
Log().Error("loading dynamic config failed", zap.Error(err)) |
|
@ -504,6 +505,10 @@ func finishSettingUp(ctx Context, cfg *Config) error { |
|
|
} |
|
|
} |
|
|
runLoadedConfig(loadedConfig) |
|
|
runLoadedConfig(loadedConfig) |
|
|
case <-ctx.Done(): |
|
|
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") |
|
|
Log().Info("stopping config load interval") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
@ -792,10 +792,15 @@ func (lb LoadBalancing) tryAgain(ctx caddy.Context, start time.Time, proxyErr er |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// otherwise, wait and try the next available host
|
|
|
// otherwise, wait and try the next available host
|
|
|
|
|
|
timer := time.NewTimer(time.Duration(lb.TryInterval)) |
|
|
select { |
|
|
select { |
|
|
case <-time.After(time.Duration(lb.TryInterval)): |
|
|
case <-timer.C: |
|
|
return true |
|
|
return true |
|
|
case <-ctx.Done(): |
|
|
case <-ctx.Done(): |
|
|
|
|
|
if !timer.Stop() { |
|
|
|
|
|
// if the timer has been stopped then read from the channel
|
|
|
|
|
|
<-timer.C |
|
|
|
|
|
} |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|