Skip to content

Commit

Permalink
fix: retry functions (#478)
Browse files Browse the repository at this point in the history
* fix the retry functions

* add comment
  • Loading branch information
friedrichwilken committed Dec 16, 2024
1 parent a213a54 commit 3684cf0
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions e2e/common/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@ 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) {
ticker := time.NewTicker(interval)
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.
Expand Down

0 comments on commit 3684cf0

Please sign in to comment.