Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public long backoff(long attempts) {
}
double exp = Math.min(attempts, this.expMax);
double term = initialInterval * Math.pow(multiplier, exp);
double randomFactor = ThreadLocalRandom.current().nextDouble(1 - jitter, 1 + jitter);
double randomFactor = jitter < Double.MIN_NORMAL ? 1.0 :

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I had to look this constant up :)

Can we just make it check if the jitter is equal to zero (or maybe <= zero)? A caller of this method setting jitter to something like 0.5 might be surprised that there is no jitter added.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

MIN_NORMAL is 2^-1022, though. So it certainly wouldn't affect someone setting jitter = 0.5.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Gotcha, I misunderstood the docs and thought it was ~1. Let's keep it as-is

ThreadLocalRandom.current().nextDouble(1 - jitter, 1 + jitter);
return (long) (randomFactor * term);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ public void testExponentialBackoff() {
}
}
}

@Test
public void testExponentialBackoffWithoutJitter() {
ExponentialBackoff exponentialBackoff = new ExponentialBackoff(100, 2, 400, 0.0);
assertEquals(100, exponentialBackoff.backoff(0));
assertEquals(200, exponentialBackoff.backoff(1));
assertEquals(400, exponentialBackoff.backoff(2));
assertEquals(400, exponentialBackoff.backoff(3));
}
}