Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retry functions #478

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading