Skip to content

Conversation

@tinnywang
Copy link

The exponential backoff algorithm truncates the exponential factor when math.Pow(base, float64(attempt)) is converted to time.Duration (the type conversion is from float64 to int64).

This makes the delay between retries not increase exponentially when the base is a non-integer.
For example, the expected vs. actual results for the first five attempts of Exponential(100 * time.Millisecond, 1.2) are shown below:

attempt expected actual
1 120ms 100ms
2 144ms 100ms
3 172.8ms 100ms
4 207.36ms 200ms
5 248.832ms 200ms

To fix this, multiply the duration factor by the exponential factor before converting anything back to time.Duration.

@Rican7
Copy link
Owner

Rican7 commented Jan 13, 2022

Oh wow, thanks for this Tiffany! Sorry that I'm just now getting to this.

Now that I think about it... oof... there's a few different places where gross conversions between float and int are happening... I think I'm going to audit all of those and re-think some things before merging this so that the change can be more consistent throughout the codebase.

Thanks for bringing this to my attention and for proposing these changes!

@Rican7 Rican7 added the Bug label Jan 13, 2022
@Rican7 Rican7 self-assigned this Jan 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants