Skip to content

Conversation

@maen-bn
Copy link

@maen-bn maen-bn commented Jan 20, 2026

Resolves #8558

@maen-bn maen-bn requested a review from a team as a code owner January 20, 2026 19:48
@maen-bn maen-bn force-pushed the add-jitter-ari-retry-after-header branch from 421c5f7 to b2251a1 Compare January 20, 2026 20:04
factor := 0.2 * (2*rand.Float64() - 1)
jittered := int(float64(base) * (1 + factor))

if jittered < 0 {
Copy link

@nmxhc nmxhc Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

I think that this condition should never be met for positive inputs since then jittered>base*0.8>0.
For negative inputs however, this creates an infinite recursion (up to rounding errors for bases close to 0) since jittered<base*0.8<0.

From my understanding of the context, I suggest to remove this branch and adjust the comment.

// createJitter will return a random integer within a 20% window of the base that is provided.
// If the jittered amount is a negative number, the jitter is retried until a positive
// number is generated
func createJitter(base int) int {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor comments:

  1. Since this is a hyper-specific helper function, it might be cleaner for this function to take a time.Duration as input and return a string-formatted integer. Then it could simply be called like
response.Header().Set(headerRetryAfter, jitterHeader(6 * time.Hour))
  1. The check for negative numbers is a bug. Even if the input is just 1, it should be impossible for this function to return anything less than 0. And if the input is negative, this will loop infinitely. I'd remove the check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add jitter to ARI Retry-After

3 participants