public static long getExpirationMillis(long now, Duration duration) { try { return duration.plusMillis(now).toMillis(); } catch (ArithmeticException e) { return Long.MAX_VALUE; } }
@Override protected Duration computeNextWaitTime() { Duration next = mNextSleep; mNextSleep = mNextSleep.multipliedBy(2); if (mNextSleep.compareTo(mMaxSleep) > 0) { mNextSleep = mMaxSleep; } // Add jitter. long jitter = Math.round(ThreadLocalRandom.current().nextDouble(0.1) * next.toMillis()); return next.plusMillis(jitter); }
jitter = random.nextLong(lowBound, highBound); Duration effectiveBackoff = nextBackoff.plusMillis(jitter); return Mono.delay(effectiveBackoff); });
long expectedMillis = Duration.ofHours(17).plusMinutes(51).plusSeconds(4).plusMillis(780).toMillis(); assertThat(c2).isEqualTo((int)expectedMillis);
/** * Returns a copy of this duration with the specified duration in milliseconds subtracted. * <p> * This instance is immutable and unaffected by this method call. * * @param millisToSubtract the milliseconds to subtract, positive or negative * @return a {@code Duration} based on this duration with the specified milliseconds subtracted, not null * @throws ArithmeticException if numeric overflow occurs */ public Duration minusMillis(long millisToSubtract) { return (millisToSubtract == Long.MIN_VALUE ? plusMillis(Long.MAX_VALUE).plusMillis(1) : plusMillis(-millisToSubtract)); }
public static long getExpirationMillis(long now, Duration duration) { try { return duration.plusMillis(now).toMillis(); } catch (ArithmeticException e) { return Long.MAX_VALUE; } }
/** * Will run all tasks scheduled since the last time this was executed + the given duration. * * @param elapsed upper bound of tasks delay */ public void runScheduledTasks(Duration elapsed) { final Duration upperBound = elapsed.plusMillis(lastRanAtMs); scheduledTasks.stream() .filter(t -> t.getDelay().compareTo(upperBound) <= 0) .sorted(Comparator.comparing(MockScheduledTask::getDelay)) .forEach(MockScheduledTask::run); lastRanAtMs = upperBound.toMillis(); } }
/** * Will run all tasks scheduled since the last time this was executed + the given duration. * * @param elapsed upper bound of tasks delay */ public void runScheduledTasks(Duration elapsed) { final Duration upperBound = elapsed.plusMillis(lastRanAtMs); scheduledTasks.stream() .filter(t -> t.getDelay().compareTo(upperBound) <= 0) .sorted(Comparator.comparing(MockScheduledTask::getDelay)) .forEach(MockScheduledTask::run); lastRanAtMs = upperBound.toMillis(); } }
@Override protected Duration computeNextWaitTime() { Duration next = mNextSleep; mNextSleep = mNextSleep.multipliedBy(2); if (mNextSleep.compareTo(mMaxSleep) > 0) { mNextSleep = mMaxSleep; } // Add jitter. long jitter = Math.round(ThreadLocalRandom.current().nextDouble(0.1) * next.toMillis()); return next.plusMillis(jitter); }
/** * Convert hh:mm:ss.mmmmmmm format to Duration * @param input * @return */ private Optional<Duration> convertDuration(String input) { final Matcher matcher = DURATION_FORMAT.matcher(input); if (matcher.matches()) { final long hh = Long.parseLong(matcher.group(1)); final long mm = Long.parseLong(matcher.group(2)); final long ss = Long.parseLong(matcher.group(3)); final long ms = Long.parseLong(matcher.group(4)); return Optional.of(Duration.ofHours(hh) .plusMinutes(mm) .plusSeconds(ss) .plusMillis(ms)); } else { log.warn(String.format("Cannot parse duration string [%s]", input)); return Optional.empty(); } }
millis = Long.parseLong(matcher.group(5)); return Duration.ofDays(days).plusHours(hours).plusMinutes(minutes).plusSeconds(seconds).plusMillis( millis);
@Override public Duration apply(BackoffDelay backoff) { //check the invariant if (backoff.delay.compareTo(backoff.min) < 0) { throw new IllegalArgumentException("jitter can only be applied on a delay that is >= to min backoff"); } if (backoff.delay.compareTo(backoff.max) > 0) { throw new IllegalArgumentException("jitter can only be applied on a delay that is <= to max backoff"); } //short-circuit delay == 0 case 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); }
jitter = random.nextLong(lowBound, highBound); Duration effectiveBackoff = nextBackoff.plusMillis(jitter); return Mono.delay(effectiveBackoff); });
counter.inc(); Thread.sleep(reportingPeriod.plusMillis(400).toMillis()); Assert.assertTrue(server.getBytesRead() > 0); Assert.assertTrue(server.contains("post adding counter", "foo.bar.baz"));
@Override public void initialize(DurationMin constraintAnnotation) { this.minDuration = Duration.ofNanos( constraintAnnotation.nanos() ) .plusMillis( constraintAnnotation.millis() ) .plusSeconds( constraintAnnotation.seconds() ) .plusMinutes( constraintAnnotation.minutes() ) .plusHours( constraintAnnotation.hours() ) .plusDays( constraintAnnotation.days() ); this.inclusive = constraintAnnotation.inclusive(); }
@Override public void initialize(DurationMax constraintAnnotation) { this.maxDuration = Duration.ofNanos( constraintAnnotation.nanos() ) .plusMillis( constraintAnnotation.millis() ) .plusSeconds( constraintAnnotation.seconds() ) .plusMinutes( constraintAnnotation.minutes() ) .plusHours( constraintAnnotation.hours() ) .plusDays( constraintAnnotation.days() ); this.inclusive = constraintAnnotation.inclusive(); }
case NANOS: return plusNanos(amountToAdd); case MICROS: return plusSeconds((amountToAdd / (1000000L * 1000)) * 1000).plusNanos((amountToAdd % (1000000L * 1000)) * 1000); case MILLIS: return plusMillis(amountToAdd); case SECONDS: return plusSeconds(amountToAdd);
.addTime(MessageObserver.MESSAGE_TIME_TO_LIVE_CHECK_INTERVAL.plusMillis(timeToLive));
.addTime(MessageObserver.MESSAGE_TIME_TO_LIVE_CHECK_INTERVAL.plusMillis(timeToLive));
long expectedMillis = Duration.ofHours(17).plusMinutes(51).plusSeconds(4).plusMillis(780).toMillis(); assertThat(c2).isEqualTo((int)expectedMillis);