Skip to content

Commit

Permalink
Fallback to default strategy in backoff.Retry(), bump version to 1.…
Browse files Browse the repository at this point in the history
…0.0 (fixes #19).
  • Loading branch information
jmalloc committed Nov 29, 2022
1 parent 1729d56 commit b1565a6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ The format is based on [Keep a Changelog], and this project adheres to
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
[semantic versioning]: https://semver.org/spec/v2.0.0.html

## [1.0.0] - 2022-11-29

### Changed

- `backoff.Retry()` will now use the default strategy if a `nil` strategy is passed

## [0.2.2] - 2022-11-29

## Fixed
Expand Down Expand Up @@ -51,6 +57,7 @@ The format is based on [Keep a Changelog], and this project adheres to
[0.2.0]: https://github.com/dogmatiq/linger/releases/tag/v0.2.0
[0.2.1]: https://github.com/dogmatiq/linger/releases/tag/v0.2.1
[0.2.2]: https://github.com/dogmatiq/linger/releases/tag/v0.2.2
[1.0.0]: https://github.com/dogmatiq/linger/releases/tag/v1.0.0

<!-- version template
## [0.0.1] - YYYY-MM-DD
Expand Down
5 changes: 5 additions & 0 deletions backoff/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
// Retry calls the given function until it succeeds.
//
// Each subsequent call is delayed according to the given backoff strategy.
// If s is nil, DefaultStrategy is used.
//
// It returns ctx.Err() if ctx is canceled before fn() succeeds.
// n is the number of times that fn() failed, even if err is non-nil.
Expand All @@ -17,6 +18,10 @@ func Retry(
s Strategy,
fn func(ctx context.Context) error,
) (n uint, err error) {
if s == nil {
s = DefaultStrategy
}

for {
err := fn(ctx)
if err == nil {
Expand Down
20 changes: 20 additions & 0 deletions backoff/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,24 @@ var _ = Describe("func Retry()", func() {
Expect(err).To(Equal(context.DeadlineExceeded))
Expect(n).To(BeNumerically("==", 1))
})

It("uses the default strategy if none is provided", func() {
count := 0

n, err := Retry(
context.Background(),
nil,
func(context.Context) error {
if count == 1 {
return nil
}

count++
return errors.New("<error>")
},
)

Expect(err).ShouldNot(HaveOccurred())
Expect(n).To(BeNumerically("==", 1))
})
})
4 changes: 2 additions & 2 deletions backoff/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
//
// It is a conservative policy favouring large delay times under the assumption
// that the operation is expensive.
var DefaultStrategy Strategy = WithTransforms(
var DefaultStrategy = WithTransforms(
Exponential(3*time.Second),
linger.FullJitter,
linger.Limiter(0, 1*time.Hour),
Expand All @@ -32,7 +32,7 @@ type Strategy func(err error, n uint) time.Duration
// The unit delay is doubled after each successive failure.
func Exponential(unit time.Duration) Strategy {
if unit <= 0 {
panic("the unit duration must be postive")
panic("the unit duration must be positive")
}

u := float64(unit)
Expand Down

0 comments on commit b1565a6

Please sign in to comment.