Skip to content

Commit

Permalink
Chore: Simplify retry functions (#335)
Browse files Browse the repository at this point in the history
Simplify retry function
  • Loading branch information
friedrichwilken authored Feb 21, 2024
1 parent a5c1eee commit d68926c
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 d68926c

Please sign in to comment.