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

delay based on error #35

Merged
merged 4 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ slightly similar as this package, don't have 'simple' `Retry` method
* [matryer/try](https://github.com/matryer/try) - very popular package,
nonintuitive interface (for me)

BREAKING CHANGES 3.0.0 * `DelayTypeFunc` have a new parameter `err` - this
breaking change affects only your custom Delay Functions (if you some have, then
JaSei marked this conversation as resolved.
Show resolved Hide resolved
change your custom functions to `(n uint, _ error, config *Config)
time.Duration` and everything will be fine)

### BREAKING CHANGES
this change allow [make delay functions based on
error](examples/delay_based_on_error_test.go)

1.0.2 -> 2.0.0

Expand Down Expand Up @@ -98,7 +103,7 @@ var (
#### func BackOffDelay

```go
func BackOffDelay(n uint, config *Config) time.Duration
func BackOffDelay(n uint, _ error, config *Config) time.Duration
```
BackOffDelay is a DelayType which increases delay between consecutive retries

Expand All @@ -111,7 +116,7 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error
#### func FixedDelay

```go
func FixedDelay(_ uint, config *Config) time.Duration
func FixedDelay(_ uint, _ error, config *Config) time.Duration
```
FixedDelay is a DelayType which keeps delay the same through all iterations

Expand All @@ -125,7 +130,7 @@ IsRecoverable checks if error is an instance of `unrecoverableError`
#### func RandomDelay

```go
func RandomDelay(_ uint, config *Config) time.Duration
func RandomDelay(_ uint, _ error, config *Config) time.Duration
```
RandomDelay is a DelayType which picks a random delay up to config.maxJitter

Expand All @@ -147,7 +152,7 @@ type Config struct {
#### type DelayTypeFunc

```go
type DelayTypeFunc func(n uint, config *Config) time.Duration
type DelayTypeFunc func(n uint, err error, config *Config) time.Duration
```


Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.0
3.0.0
2 changes: 1 addition & 1 deletion examples/custom_retry_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestCustomRetryFunction(t *testing.T) {

return err
},
retry.DelayType(func(n uint, config *retry.Config) time.Duration {
retry.DelayType(func(n uint, _ error, config *retry.Config) time.Duration {
return 0
}),
)
Expand Down
108 changes: 108 additions & 0 deletions examples/delay_based_on_error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// This test delay is based on kind of error
// e.g. HTTP response [Retry-After](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After)
package retry_test

import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"testing"
"time"

"github.com/avast/retry-go"
"github.com/stretchr/testify/assert"
)

func TestParseRetryAfter(t *testing.T) {
retryAfter, err := ParseRetryAfter("xyz")
assert.Error(t, err)

retryAfter, err = ParseRetryAfter("")
assert.Error(t, err)

testDur := 120 * time.Second

retryAfter, err = ParseRetryAfter("120")
assert.NoError(t, err)
assert.Equal(t, testDur, retryAfter, "time in seconds")

retryAfter, err = ParseRetryAfter(time.Now().Add(testDur).Format(time.RFC850))
assert.NoError(t, err)
assert.True(t, retryAfter > (115*time.Second), "time in seconds are ~120")
t.Log(retryAfter)
}

func ParseRetryAfter(ra string) (time.Duration, error) {
if ra == "" {
return 0, fmt.Errorf("Retry-After header was empty")
}

t, errParse := http.ParseTime(ra)
if errParse != nil {
if sec, errParse := strconv.Atoi(ra); errParse == nil {
return time.Duration(sec) * time.Second, nil
}
} else {
return t.Sub(time.Now()), nil
}

return 0, fmt.Errorf("Invalid Retr-After format %s", ra)
}

type RetryAfterError struct {
response http.Response
}

func (err RetryAfterError) Error() string {
return fmt.Sprintf("Request to %s fail %s (%d)", err.response.Request.RequestURI, err.response.Status, err.response.StatusCode)
}

type SomeOtherError struct {
err string
retryAfter time.Duration
}

func (err SomeOtherError) Error() string {
return err.err
}

func TestCustomRetryFunctionBasedOnKindOfError(t *testing.T) {
url := "http://example.com"
JaSei marked this conversation as resolved.
Show resolved Hide resolved
var body []byte

err := retry.Do(
func() error {
resp, err := http.Get(url)

if err == nil {
defer func() {
if err := resp.Body.Close(); err != nil {
panic(err)
}
}()
body, err = ioutil.ReadAll(resp.Body)
}

return err
},
retry.DelayType(func(n uint, err error, config *retry.Config) time.Duration {
switch err.(type) {
case RetryAfterError:
e := err.(RetryAfterError)
if dur, err := ParseRetryAfter(e.response.Header.Get("Retry-After")); err == nil {
return dur
}
case SomeOtherError:
e := err.(SomeOtherError)
return e.retryAfter
}

//default is backoffdelay
return retry.BackOffDelay(n, err, config)
}),
)

assert.NoError(t, err)
assert.NotEmpty(t, body)
}
15 changes: 9 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type RetryIfFunc func(error) bool
// n = count of attempts
type OnRetryFunc func(n uint, err error)

type DelayTypeFunc func(n uint, config *Config) time.Duration
type DelayTypeFunc func(n uint, err error, config *Config) time.Duration
JaSei marked this conversation as resolved.
Show resolved Hide resolved

type Config struct {
attempts uint
Expand Down Expand Up @@ -81,45 +81,48 @@ func DelayType(delayType DelayTypeFunc) Option {
}

// BackOffDelay is a DelayType which increases delay between consecutive retries
func BackOffDelay(n uint, config *Config) time.Duration {
func BackOffDelay(n uint, _ error, config *Config) time.Duration {
// 1 << 63 would overflow signed int64 (time.Duration), thus 62.
const max uint = 62

if config.maxBackOffN == 0 {
if config.delay <= 0 {
config.delay = 1
}

config.maxBackOffN = max - uint(math.Floor(math.Log2(float64(config.delay))))
}

if n > config.maxBackOffN {
n = config.maxBackOffN
}

return config.delay << n
}

// FixedDelay is a DelayType which keeps delay the same through all iterations
func FixedDelay(_ uint, config *Config) time.Duration {
func FixedDelay(_ uint, _ error, config *Config) time.Duration {
return config.delay
}

// RandomDelay is a DelayType which picks a random delay up to config.maxJitter
func RandomDelay(_ uint, config *Config) time.Duration {
func RandomDelay(_ uint, _ error, config *Config) time.Duration {
return time.Duration(rand.Int63n(int64(config.maxJitter)))
}

// CombineDelay is a DelayType the combines all of the specified delays into a new DelayTypeFunc
func CombineDelay(delays ...DelayTypeFunc) DelayTypeFunc {
const maxInt64 = uint64(math.MaxInt64)

return func(n uint, config *Config) time.Duration {
return func(n uint, err error, config *Config) time.Duration {
var total uint64
for _, delay := range delays {
total += uint64(delay(n, config))
total += uint64(delay(n, err, config))
if total > maxInt64 {
total = maxInt64
}
}

return time.Duration(total)
}
}
Expand Down
8 changes: 7 additions & 1 deletion retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ SEE ALSO
* [matryer/try](https://github.com/matryer/try) - very popular package, nonintuitive interface (for me)

BREAKING CHANGES
JaSei marked this conversation as resolved.
Show resolved Hide resolved
3.0.0
* `DelayTypeFunc` have a new parameter `err` - this breaking change affects only your custom Delay Functions
(if you some have, then change your custom functions to `(n uint, _ error, config *Config) time.Duration` and everything will be fine)

this change allow [make delay functions based on error](examples/delay_based_on_error_test.go)


1.0.2 -> 2.0.0

Expand Down Expand Up @@ -134,7 +140,7 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
break
}

delayTime := config.delayType(n, config)
delayTime := config.delayType(n, err, config)
if config.maxDelay > 0 && delayTime > config.maxDelay {
delayTime = config.maxDelay
}
Expand Down
6 changes: 3 additions & 3 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestBackOffDelay(t *testing.T) {
config := Config{
delay: c.delay,
}
delay := BackOffDelay(c.n, &config)
delay := BackOffDelay(c.n, nil, &config)
assert.Equal(t, c.expectedMaxN, config.maxBackOffN, "max n mismatch")
assert.Equal(t, c.expectedDelay, delay, "delay duration mismatch")
},
Expand All @@ -209,7 +209,7 @@ func TestBackOffDelay(t *testing.T) {

func TestCombineDelay(t *testing.T) {
f := func(d time.Duration) DelayTypeFunc {
return func(_ uint, _ *Config) time.Duration {
return func(_ uint, _ error, _ *Config) time.Duration {
return d
}
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func TestCombineDelay(t *testing.T) {
for i, d := range c.delays {
funcs[i] = f(d)
}
actual := CombineDelay(funcs...)(0, nil)
actual := CombineDelay(funcs...)(0, nil, nil)
assert.Equal(t, c.expected, actual, "delay duration mismatch")
},
)
Expand Down