diff --git a/README.md b/README.md index 80fb73b..5431b4a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/VERSION b/VERSION index 4a36342..fd2a018 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.0.0 +3.1.0 diff --git a/retry.go b/retry.go index af2d926..b5ef9f0 100644 --- a/retry.go +++ b/retry.go @@ -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 { @@ -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 diff --git a/retry_test.go b/retry_test.go index a9e9536..c7bee3a 100644 --- a/retry_test.go +++ b/retry_test.go @@ -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() @@ -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") })