-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: update dependencies * feat: add wrapper over new backoff pkg * feat: modify retryPolicy to use new backoff pkg * test: update tests * feat: use builder pattern for backoff * fix: remove unused code * feat: add IsError method to the response struct
- Loading branch information
Showing
6 changed files
with
87 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package backoff | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
) | ||
|
||
const ( | ||
exponentialMultiplier = 2 | ||
) | ||
|
||
type BackOff struct { | ||
maxAttempts int | ||
maxDuration time.Duration | ||
initialDuration time.Duration | ||
} | ||
|
||
func New() *BackOff { | ||
return &BackOff{} | ||
} | ||
|
||
func (b *BackOff) WithMaxAttempts(maxAttempts int) *BackOff { | ||
b.maxAttempts = maxAttempts | ||
return b | ||
} | ||
func (b *BackOff) WithMaxDuration(max time.Duration) *BackOff { | ||
b.maxDuration = max | ||
return b | ||
} | ||
func (b *BackOff) WithInitialDuration(initial time.Duration) *BackOff { | ||
b.initialDuration = initial | ||
return b | ||
} | ||
|
||
func (b *BackOff) Exponential() backoff.BackOff { | ||
tmp := backoff.NewExponentialBackOff(backoff.WithInitialInterval(b.initialDuration), backoff.WithMaxElapsedTime(b.maxDuration), backoff.WithMultiplier(exponentialMultiplier)) | ||
return backoff.WithMaxRetries(tmp, uint64(b.maxAttempts)) | ||
} | ||
|
||
func (b *BackOff) Linear() backoff.BackOff { | ||
return backoff.WithMaxRetries(backoff.NewConstantBackOff(b.initialDuration), uint64(b.maxAttempts)) | ||
} | ||
|
||
// Do retries op if it returns an error according to the provided backoff | ||
func Do(op func() error, b backoff.BackOff) error { | ||
return backoff.Retry(op, b) | ||
} |
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