From e564e3e553f63cd8a5bef2f9d7f221ce4266b4df Mon Sep 17 00:00:00 2001 From: Friedrich Date: Mon, 16 Dec 2024 12:14:43 +0100 Subject: [PATCH] fix: retry functions (#478) * fix the retry functions * add comment --- e2e/common/retry.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/e2e/common/retry.go b/e2e/common/retry.go index 9c8aee5d..a69d5cfb 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 // Wait for the ticker interval. attempts-- err = fn() - if err == nil || attempts == 0 { - return err + if err == nil { + return nil } } - // Return nil if all attempts fail. - return nil + // Return the err if all attempts fail. + 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 // Wait for the ticker interval. 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.