Skip to content

Commit b1565a6

Browse files
committed
Fallback to default strategy in backoff.Retry(), bump version to 1.0.0 (fixes #19).
1 parent 1729d56 commit b1565a6

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

Diff for: CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ The format is based on [Keep a Changelog], and this project adheres to
1010
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
1111
[semantic versioning]: https://semver.org/spec/v2.0.0.html
1212

13+
## [1.0.0] - 2022-11-29
14+
15+
### Changed
16+
17+
- `backoff.Retry()` will now use the default strategy if a `nil` strategy is passed
18+
1319
## [0.2.2] - 2022-11-29
1420

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

5562
<!-- version template
5663
## [0.0.1] - YYYY-MM-DD

Diff for: backoff/retry.go

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
// Retry calls the given function until it succeeds.
1010
//
1111
// Each subsequent call is delayed according to the given backoff strategy.
12+
// If s is nil, DefaultStrategy is used.
1213
//
1314
// It returns ctx.Err() if ctx is canceled before fn() succeeds.
1415
// n is the number of times that fn() failed, even if err is non-nil.
@@ -17,6 +18,10 @@ func Retry(
1718
s Strategy,
1819
fn func(ctx context.Context) error,
1920
) (n uint, err error) {
21+
if s == nil {
22+
s = DefaultStrategy
23+
}
24+
2025
for {
2126
err := fn(ctx)
2227
if err == nil {

Diff for: backoff/retry_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,24 @@ var _ = Describe("func Retry()", func() {
4646
Expect(err).To(Equal(context.DeadlineExceeded))
4747
Expect(n).To(BeNumerically("==", 1))
4848
})
49+
50+
It("uses the default strategy if none is provided", func() {
51+
count := 0
52+
53+
n, err := Retry(
54+
context.Background(),
55+
nil,
56+
func(context.Context) error {
57+
if count == 1 {
58+
return nil
59+
}
60+
61+
count++
62+
return errors.New("<error>")
63+
},
64+
)
65+
66+
Expect(err).ShouldNot(HaveOccurred())
67+
Expect(n).To(BeNumerically("==", 1))
68+
})
4969
})

Diff for: backoff/strategy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
//
1212
// It is a conservative policy favouring large delay times under the assumption
1313
// that the operation is expensive.
14-
var DefaultStrategy Strategy = WithTransforms(
14+
var DefaultStrategy = WithTransforms(
1515
Exponential(3*time.Second),
1616
linger.FullJitter,
1717
linger.Limiter(0, 1*time.Hour),
@@ -32,7 +32,7 @@ type Strategy func(err error, n uint) time.Duration
3232
// The unit delay is doubled after each successive failure.
3333
func Exponential(unit time.Duration) Strategy {
3434
if unit <= 0 {
35-
panic("the unit duration must be postive")
35+
panic("the unit duration must be positive")
3636
}
3737

3838
u := float64(unit)

0 commit comments

Comments
 (0)