Skip to content
Draft
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 @@ -37,7 +37,7 @@ public ItemExponentialFailureRateLimiter(Duration baseDelay, Duration maxDelay)
public Duration when(T item) {
long exp = failures.computeIfAbsent(item, k -> new AtomicLong()).getAndIncrement();
long d = maxDelay.toMillis() >> exp;
return d > baseDelay.toMillis() ? baseDelay.multipliedBy(1 << exp) : maxDelay;
return d > baseDelay.toMillis() ? baseDelay.multipliedBy(1L << exp) : maxDelay;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,23 @@ void negativeMaxDelay() {
assertThat(rateLimiter.when("one")).isEqualTo(Duration.ofSeconds(-1000));
assertThat(rateLimiter.when("one")).isEqualTo(Duration.ofSeconds(-1000));
}

@Test
void highFailureCountStaysPositive() {
// maxDelay is large enough that the baseDelay*2^exp branch is still selected at exp == 31.
// The multiplier must be computed in long arithmetic; an int shift (1 << 31) overflows to
// Integer.MIN_VALUE and yields a negative backoff, which is never a valid delay.
Duration baseDelay = Duration.ofMillis(1);
Duration maxDelay = Duration.ofMillis(1L << 40);
RateLimiter<String> rateLimiter = new ItemExponentialFailureRateLimiter<>(baseDelay, maxDelay);

// Advance to exp == 31 (the 32nd call returns the delay computed with exp == 31).
for (int i = 0; i < 31; i++) {
rateLimiter.when("item");
}

Duration backoff = rateLimiter.when("item");
assertThat(backoff).isPositive();
assertThat(backoff).isEqualTo(baseDelay.multipliedBy(1L << 31));
}
}
Loading