From ad10a5c92ca19a8b37a526737f4b8f5aba1c6aec Mon Sep 17 00:00:00 2001 From: Friedrich Wilken Date: Mon, 16 Dec 2024 11:07:36 +0100 Subject: [PATCH] fix the retry functions --- e2e/common/retry.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/e2e/common/retry.go b/e2e/common/retry.go index 9c8aee5d..5721ee5b 100644 --- a/e2e/common/retry.go +++ b/e2e/common/retry.go @@ -8,15 +8,17 @@ func Retry(attempts int, interval time.Duration, fn func() error) error { ticker := time.NewTicker(interval) defer ticker.Stop() var err error - for range ticker.C { + + for attempts > 0 { + <-ticker.C attempts-- err = fn() - if err == nil || attempts == 0 { - return err + if err == nil { + return nil } } // Return nil if all attempts fail. - return nil + return err } func RetryGet[T any](attempts int, interval time.Duration, fn func() (*T, error)) (*T, error) { @@ -24,11 +26,13 @@ func RetryGet[T any](attempts int, interval time.Duration, fn func() (*T, error) defer ticker.Stop() var err error var obj *T - for range ticker.C { + + for attempts > 0 { + <-ticker.C attempts-- obj, err = fn() - if err == nil || attempts == 0 { - return obj, err + if err == nil { + return obj, nil } } // Return nil object if all attempts fail.