summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorДенис Телюх <telyukh.denis@gmail.com>2022-01-05 02:14:18 +0700
committerGitHub <noreply@github.com>2022-01-04 12:14:18 -0700
commit2e46c2ac1d860230a80de5fd7bc85c8c9429d3f4 (patch)
tree981f0fc3fd271a62f7ff82f71718b0c34271d2d2
parent249adc1c872ae46e640cfb7c330e332229d8d32a (diff)
admin, reverseproxy: Stop timers if canceled to avoid goroutine leak (#4482)
-rw-r--r--caddy.go7
-rw-r--r--modules/caddyhttp/reverseproxy/reverseproxy.go7
2 files changed, 12 insertions, 2 deletions
diff --git a/caddy.go b/caddy.go
index bee4274..f990d36 100644
--- a/caddy.go
+++ b/caddy.go
@@ -494,9 +494,10 @@ 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))
@@ -504,6 +505,10 @@ func finishSettingUp(ctx Context, cfg *Config) error {
}
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
}
diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go
index b418953..eaa7cbf 100644
--- a/modules/caddyhttp/reverseproxy/reverseproxy.go
+++ b/modules/caddyhttp/reverseproxy/reverseproxy.go
@@ -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
}
}