Skip to content

Commit

Permalink
Refactor retry to avoid linter warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
friedrichwilken committed Dec 9, 2023
1 parent f8fb12d commit bbcd5f8
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions e2e/common/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ 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 attempts > 0 {
<-ticker.C
attempts--
err = fn()
if err == nil {
return nil
}
}
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 { //nolint:gosimple//There is no range here.
select {
case <-ticker.C:
attempts--
obj, err = fn()
if err == nil || attempts == 0 {
return obj, err
}

for attempts > 0 {
<-ticker.C
attempts--
obj, err = fn()
if err == nil {
return obj, nil
}
}
return obj, err
}

0 comments on commit bbcd5f8

Please sign in to comment.