Repository navigation
fix: keep EqualJitter delays within [cap/2, cap) - #7
Merged
Merged
Conversation
nodivbyzero
approved these changes
Oct 5, 2026
nodivbyzero
left a comment
Owner
There was a problem hiding this comment.
Thank you for the contribution.
The fix looks good to me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
EqualJitteris documented (on the constant and indoc.go) ascap/2 + rand[0, cap/2), which keeps every delay in[cap/2, cap). ButcalculateNextDelayused the whole backoff cap as the jitter window for both strategies.EqualJitterthen addedrand[0, cap)on top of thecap/2base, so a delay could reach almost1.5 × cap. The only thing holding it back wasMaxDelay, so the overshoot showed up any time the exponential cap was still underMaxDelay. That covers every early retry with the default 30s ceiling.Example: with
WithInitialDelay(100ms)andWithJitter(EqualJitter), the first retry could wait up to ~150ms, the second up to ~300ms, and so on.Change
EqualJitter, the default jitter window is now the other half of the cap (cap - cap/2).WithMaxJittercan still shrink it. The base stays atcap/2, soTestDo_WithMaxJitter_EqualJitter_PreservesBasestill holds.FullJitteris unchanged.MaxDelayclamp is now only there for the 1ms floors.Test
TestDo_EqualJitter_StaysWithinCapruns 200 rounds withMaxDelayfar above the cap and checks that everyRetryInfo.Delaylands in[cap/2, cap). It fails onmainand passes with this change.go test -race ./...andgo vet ./...pass.