-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
69 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/cenkalti/backoff/v3" | ||
"log" | ||
"time" | ||
) | ||
|
||
// WithDefaultRetry retries a given function with a default exponential backoff | ||
func WithDefaultRetry(do func() error) error { | ||
return WithRetry(do, 2*time.Second, 10*time.Second, 5*time.Minute) | ||
} | ||
|
||
// WithRetry retries a given function with an exponential backoff until maxTime is reached | ||
func WithRetry(do func() error, interval, maxInterval, maxTime time.Duration) error { | ||
exponential := backoff.NewExponentialBackOff() | ||
exponential.MaxElapsedTime = maxTime | ||
exponential.InitialInterval = interval | ||
exponential.MaxInterval = maxInterval | ||
|
||
err := backoff.RetryNotify(do, exponential, func(err error, d time.Duration) { | ||
log.Printf("call failed, retrying in %s. Error: %v", d.Round(time.Second), err) | ||
}) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters