@Test void applyToDelayUnderMinRejected() { Duration min = Duration.ofMillis(100); Duration max = Duration.ofMillis(100); Duration delay = Duration.ofMillis(99); BackoffDelay bd = new BackoffDelay(min, max, delay); RandomJitter jitter = new RandomJitter(0.5); assertThatExceptionOfType(InvalidArgumentException.class).isThrownBy(() -> jitter.apply(bd)) .withMessage("jitter can only be applied on a delay that is >= to min backoff"); }
@Override public Duration apply(BackoffDelay backoff) { if (backoff.delay.compareTo(backoff.min) < 0) { throw InvalidArgumentException.fromMessage("jitter can only be applied on a delay that is >= to min backoff"); } if (backoff.delay.compareTo(backoff.max) > 0) { throw InvalidArgumentException.fromMessage("jitter can only be applied on a delay that is <= to max backoff"); } if (backoff.delay.isZero()) { return backoff.delay; } ThreadLocalRandom random = ThreadLocalRandom.current(); long jitterOffset = jitterOffsetCapped(backoff); long lowBound = lowJitterBound(backoff, jitterOffset); long highBound = highJitterBound(backoff, jitterOffset); long jitter; if (highBound == lowBound) { if (highBound == 0) jitter = 0; else jitter = random.nextLong(highBound); } else { jitter = random.nextLong(lowBound, highBound); } return backoff.delay.plusMillis(jitter); }
RandomJitter implements Jitter { @Override public Duration apply(BackoffDelay backoff) { if (backoff.delay.compareTo(backoff.min) < 0) { throw InvalidArgumentException.fromMessage("jitter can only be applied on a delay that is >= to min backoff"); } if (backoff.delay.compareTo(backoff.max) > 0) { throw InvalidArgumentException.fromMessage("jitter can only be applied on a delay that is <= to max backoff"); } if (backoff.delay.isZero()) { return backoff.delay; } ThreadLocalRandom random = ThreadLocalRandom.current(); long jitterOffset = jitterOffsetCapped(backoff); long lowBound = lowJitterBound(backoff, jitterOffset); long highBound = highJitterBound(backoff, jitterOffset); long jitter; if (highBound == lowBound) { if (highBound == 0) jitter = 0; else jitter = random.nextLong(highBound); } else { jitter = random.nextLong(lowBound, highBound); } return backoff.delay.plusMillis(jitter); } }
RandomJitter implements Jitter { @Override public Duration apply(BackoffDelay backoff) { if (backoff.delay.compareTo(backoff.min) < 0) { throw InvalidArgumentException.fromMessage("jitter can only be applied on a delay that is >= to min backoff"); } if (backoff.delay.compareTo(backoff.max) > 0) { throw InvalidArgumentException.fromMessage("jitter can only be applied on a delay that is <= to max backoff"); } if (backoff.delay.isZero()) { return backoff.delay; } ThreadLocalRandom random = ThreadLocalRandom.current(); long jitterOffset = jitterOffsetCapped(backoff); long lowBound = lowJitterBound(backoff, jitterOffset); long highBound = highJitterBound(backoff, jitterOffset); long jitter; if (highBound == lowBound) { if (highBound == 0) jitter = 0; else jitter = random.nextLong(highBound); } else { jitter = random.nextLong(lowBound, highBound); } return backoff.delay.plusMillis(jitter); } RandomJitter(double randomFactor); }
RandomJitter implements Jitter { @Override public Duration apply(BackoffDelay backoff) { if (backoff.delay.compareTo(backoff.min) < 0) { throw InvalidArgumentException.fromMessage("jitter can only be applied on a delay that is >= to min backoff"); } if (backoff.delay.compareTo(backoff.max) > 0) { throw InvalidArgumentException.fromMessage("jitter can only be applied on a delay that is <= to max backoff"); } if (backoff.delay.isZero()) { return backoff.delay; } ThreadLocalRandom random = ThreadLocalRandom.current(); long jitterOffset = jitterOffsetCapped(backoff); long lowBound = lowJitterBound(backoff, jitterOffset); long highBound = highJitterBound(backoff, jitterOffset); long jitter; if (highBound == lowBound) { if (highBound == 0) jitter = 0; else jitter = random.nextLong(highBound); } else { jitter = random.nextLong(lowBound, highBound); } return backoff.delay.plusMillis(jitter); } RandomJitter(double randomFactor); @Override Duration apply(BackoffDelay backoff); @Override String toString(); }
RandomJitter implements Jitter { @Override public Duration apply(BackoffDelay backoff) { if (backoff.delay.compareTo(backoff.min) < 0) { throw InvalidArgumentException.fromMessage("jitter can only be applied on a delay that is >= to min backoff"); } if (backoff.delay.compareTo(backoff.max) > 0) { throw InvalidArgumentException.fromMessage("jitter can only be applied on a delay that is <= to max backoff"); } if (backoff.delay.isZero()) { return backoff.delay; } ThreadLocalRandom random = ThreadLocalRandom.current(); long jitterOffset = jitterOffsetCapped(backoff); long lowBound = lowJitterBound(backoff, jitterOffset); long highBound = highJitterBound(backoff, jitterOffset); long jitter; if (highBound == lowBound) { if (highBound == 0) jitter = 0; else jitter = random.nextLong(highBound); } else { jitter = random.nextLong(lowBound, highBound); } return backoff.delay.plusMillis(jitter); } RandomJitter(double randomFactor); @Override Duration apply(BackoffDelay backoff); @Override String toString();  }