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

moves exported default values to function #44

Merged
merged 2 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
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
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,6 @@ now implement via functions produces Options (aka `retry.OnRetry`)

## Usage

```go
var (
DefaultAttempts = uint(10)
DefaultDelay = 100 * time.Millisecond
DefaultMaxJitter = 100 * time.Millisecond
DefaultOnRetry = func(n uint, err error) {}
DefaultRetryIf = IsRecoverable
DefaultDelayType = CombineDelay(BackOffDelay, RandomDelay)
DefaultLastErrorOnly = false
DefaultContext = context.Background()
)
```

#### func BackOffDelay

```go
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.0
3.1.0
35 changes: 14 additions & 21 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,11 @@ import (
// Function signature of retryable function
type RetryableFunc func() error

var (
DefaultAttempts = uint(10)
DefaultDelay = 100 * time.Millisecond
DefaultMaxJitter = 100 * time.Millisecond
DefaultOnRetry = func(n uint, err error) {}
DefaultRetryIf = IsRecoverable
DefaultDelayType = CombineDelay(BackOffDelay, RandomDelay)
DefaultLastErrorOnly = false
DefaultContext = context.Background()
)

func Do(retryableFunc RetryableFunc, opts ...Option) error {
var n uint

//default
config := &Config{
attempts: DefaultAttempts,
delay: DefaultDelay,
maxJitter: DefaultMaxJitter,
onRetry: DefaultOnRetry,
retryIf: DefaultRetryIf,
delayType: DefaultDelayType,
lastErrorOnly: DefaultLastErrorOnly,
context: DefaultContext,
}
config := newDefaultRetryConfig()

//apply opts
for _, opt := range opts {
Expand Down Expand Up @@ -167,6 +147,19 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
return errorLog
}

func newDefaultRetryConfig() *Config {
return &Config{
attempts: uint(10),
delay: 100 * time.Millisecond,
maxJitter: 100 * time.Millisecond,
onRetry: func(n uint, err error) {},
retryIf: IsRecoverable,
delayType: CombineDelay(BackOffDelay, RandomDelay),
lastErrorOnly: false,
context: context.Background(),
}
}

// Error type represents list of errors in retry
type Error []error

Expand Down
3 changes: 2 additions & 1 deletion retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func TestCombineDelay(t *testing.T) {
}

func TestContext(t *testing.T) {
const defaultDelay = 100 * time.Millisecond
t.Run("cancel before", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
Expand All @@ -275,7 +276,7 @@ func TestContext(t *testing.T) {
)
dur := time.Since(start)
assert.Error(t, err)
assert.True(t, dur < DefaultDelay, "immediately cancellation")
assert.True(t, dur < defaultDelay, "immediately cancellation")
assert.Equal(t, 0, retrySum, "called at most once")
})

Expand Down