Skip to content

Commit

Permalink
Simplify retry function
Browse files Browse the repository at this point in the history
  • Loading branch information
friedrichwilken committed Feb 20, 2024
1 parent 8f51ee3 commit 38f453c
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions e2e/common/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,29 @@ 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) {
ticker := time.NewTicker(interval)
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
}

0 comments on commit 38f453c

Please sign in to comment.