Skip to content

Commit

Permalink
Merge pull request #44 from Noroth/feature/move-exported-values-to-de…
Browse files Browse the repository at this point in the history
…fault-function

moves exported default values to function
  • Loading branch information
JaSei authored Jun 1, 2021
2 parents 7096610 + b6d0474 commit d8dc92d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 36 deletions.
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

0 comments on commit d8dc92d

Please sign in to comment.