Skip to content

Commit

Permalink
Calculate exponential backoff iff current backoff is lower than maxBa…
Browse files Browse the repository at this point in the history
…ckoff
  • Loading branch information
ibusse authored and sethvargo committed Oct 30, 2017
1 parent cdf5dca commit a0b6743
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 10 additions & 5 deletions config/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,19 @@ func (c *RetryConfig) RetryFunc() RetryFunc {
return false, 0
}

base := math.Pow(2, float64(retry))
sleep := time.Duration(base) * TimeDurationVal(c.Backoff)

baseSleep := TimeDurationVal(c.Backoff)
maxSleep := TimeDurationVal(c.MaxBackoff)
if maxSleep > 0 && maxSleep < sleep {
return true, maxSleep

if maxSleep > 0 {
attemptsTillMaxBackoff := int(math.Log2(maxSleep.Seconds() / baseSleep.Seconds()))
if retry > attemptsTillMaxBackoff {
return true, maxSleep
}
}

base := math.Pow(2, float64(retry))
sleep := time.Duration(base) * baseSleep

return true, sleep
}
}
Expand Down
11 changes: 11 additions & 0 deletions config/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ func TestRetryFunc(t *testing.T) {
Bool(true),
TimeDuration(5 * time.Second),
},
{
"max backoff, attempt 100",
&RetryConfig{
Attempts: Int(0),
Backoff: TimeDuration(1 * time.Millisecond),
MaxBackoff: TimeDuration(2 * time.Millisecond),
},
Int(100),
Bool(true),
TimeDuration(2 * time.Millisecond),
},
}

for i, tc := range cases {
Expand Down

0 comments on commit a0b6743

Please sign in to comment.