From 2835a11824660b26ea665562126c35c222118d89 Mon Sep 17 00:00:00 2001 From: Aaron Schlesinger Date: Mon, 29 Mar 2021 15:26:53 -0700 Subject: [PATCH 1/3] using time.Timers instead of time.After() Signed-off-by: Aaron Schlesinger --- pkg/scalers/external_scaler.go | 13 +++++++++---- pkg/scaling/scale_handler.go | 5 ++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/scalers/external_scaler.go b/pkg/scalers/external_scaler.go index 9063aec9335..eb7c73a69d0 100644 --- a/pkg/scalers/external_scaler.go +++ b/pkg/scalers/external_scaler.go @@ -231,23 +231,28 @@ func (s *externalPushScaler) Run(ctx context.Context, active chan<- bool) { // retry on error from runWithLog() starting by 2 sec backing off * 2 with a max of 1 minute retryDuration := time.Second * 2 - retryBackoff := func() <-chan time.Time { - ch := time.After(retryDuration) + // the caller of this function needs to ensure that they call Stop() on the resulting + // timer, to release background resources. + retryBackoff := func() *time.Timer { + tmr := time.NewTimer(retryDuration) retryDuration *= time.Second * 2 if retryDuration > time.Minute*1 { retryDuration = time.Minute * 1 } - return ch + return tmr } // start the first run without delay runWithLog() for { + backoffTimer := retryBackoff() select { case <-ctx.Done(): + backoffTimer.Stop() return - case <-retryBackoff(): + case <-backoffTimer.C: + backoffTimer.Stop() runWithLog() } } diff --git a/pkg/scaling/scale_handler.go b/pkg/scaling/scale_handler.go index 9d11761f2b0..df542a44c89 100644 --- a/pkg/scaling/scale_handler.go +++ b/pkg/scaling/scale_handler.go @@ -140,12 +140,15 @@ func (h *scaleHandler) startScaleLoop(ctx context.Context, withTriggers *kedav1a pollingInterval := getPollingInterval(withTriggers) logger.V(1).Info("Watching with pollingInterval", "PollingInterval", pollingInterval) + tmr := time.NewTimer(pollingInterval) for { select { - case <-time.After(pollingInterval): + case <-tmr.C: h.checkScalers(ctx, scalableObject, scalingMutex) + tmr.Stop() case <-ctx.Done(): logger.V(1).Info("Context canceled") + tmr.Stop() return } } From b56836e1328a983d1c1985ee203c7b4d993fbb18 Mon Sep 17 00:00:00 2001 From: Aaron Schlesinger Date: Mon, 29 Mar 2021 15:31:06 -0700 Subject: [PATCH 2/3] adding entry to changelog Signed-off-by: Aaron Schlesinger --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07ca71af073..1e210e55874 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ ## Unreleased - Add OpenStack Metrics Scaler ([#1382](https://github.com/kedacore/keda/issues/1382)) +- Fixed goroutine leaks in usage of timers ### New From b3d8a92a6e789370a3b617ebfea14649f918d93e Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com> Date: Tue, 6 Apr 2021 09:38:48 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e210e55874..4184788c8ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ ## Unreleased - Add OpenStack Metrics Scaler ([#1382](https://github.com/kedacore/keda/issues/1382)) -- Fixed goroutine leaks in usage of timers +- Fixed goroutine leaks in usage of timers ([#1704](https://github.com/kedacore/keda/pull/1704)) ### New