From d68926c1d723753e649c489c55a370183eabb478 Mon Sep 17 00:00:00 2001 From: Friedrich Date: Wed, 21 Feb 2024 08:32:35 +0100 Subject: [PATCH] Chore: Simplify retry functions (#335) Simplify retry function --- e2e/common/retry.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/e2e/common/retry.go b/e2e/common/retry.go index ea831b98..9c8aee5d 100644 --- a/e2e/common/retry.go +++ b/e2e/common/retry.go @@ -8,16 +8,15 @@ func Retry(attempts int, interval time.Duration, fn func() error) error { ticker := time.NewTicker(interval) defer ticker.Stop() var err error - for { //nolint:gosimple//There is no range here. - select { - case <-ticker.C: - attempts-- - err = fn() - if err == nil || attempts == 0 { - return err - } + for range ticker.C { + attempts-- + err = fn() + if err == nil || attempts == 0 { + return err } } + // Return nil if all attempts fail. + return nil } func RetryGet[T any](attempts int, interval time.Duration, fn func() (*T, error)) (*T, error) { @@ -25,14 +24,13 @@ func RetryGet[T any](attempts int, interval time.Duration, fn func() (*T, error) defer ticker.Stop() var err error var obj *T - for { //nolint:gosimple//There is no range here. - select { - case <-ticker.C: - attempts-- - obj, err = fn() - if err == nil || attempts == 0 { - return obj, err - } + for range ticker.C { + attempts-- + obj, err = fn() + if err == nil || attempts == 0 { + return obj, err } } + // Return nil object if all attempts fail. + return nil, err }