Skip to content

Commit

Permalink
fix: jitter must be >= 1ns (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
segevda committed Mar 19, 2023
1 parent 9943eab commit 38b1644
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions retry.go
Expand Up @@ -209,6 +209,9 @@ func jitterBackoff(min, max time.Duration, attempt int) time.Duration {

temp := math.Min(capLevel, base*math.Exp2(float64(attempt)))
ri := time.Duration(temp / 2)
if ri == 0 {
ri = time.Nanosecond
}
result := randDuration(ri)

if result < min {
Expand Down
14 changes: 14 additions & 0 deletions retry_test.go
Expand Up @@ -295,6 +295,20 @@ func TestClientRetryWaitMaxInfinite(t *testing.T) {
}
}

func TestClientRetryWaitMaxMinimum(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()

const retryMaxWaitTime = time.Nanosecond // minimal duration value

c := dc().
SetRetryCount(1).
SetRetryMaxWaitTime(retryMaxWaitTime).
AddRetryCondition(func(*Response, error) bool { return true })
_, err := c.R().Get(ts.URL + "/set-retrywaittime-test")
assertError(t, err)
}

func TestClientRetryWaitCallbackError(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()
Expand Down

0 comments on commit 38b1644

Please sign in to comment.