@Override public Instant sleepPast(Instant futureTime) throws InterruptedException { Instant now; ChronoUnit unit = getUnit(); /* * Distributed storage managers that rely on timestamps play with the * least significant bit in timestamp longs, turning it on or off to * ensure that deletions are logically ordered before additions within a * single batch mutation. This is not a problem at microsecond * resolution because we pretendulate microsecond resolution by * multiplying currentTimeMillis by 1000, so the LSB can vary freely. * It's also not a problem with nanosecond resolution because the * resolution is just too fine, relative to how long a mutation takes, * for it to matter in practice. But it can lead to corruption at * millisecond resolution (and does, in testing). */ if (unit.equals(TimeUnit.MILLISECONDS)) futureTime = futureTime.plusMillis(1L); while ((now = getTime()).compareTo(futureTime) <= 0) { long delta = getTime(futureTime) - getTime(now); if (0L == delta) delta = 1L; if (log.isTraceEnabled()) { log.trace("Sleeping: now={} targettime={} delta={} {}", new Object[] { now, futureTime, delta, unit }); } Temporals.timeUnit(unit).sleep(delta); } return now; }
@Override public Instant sleepPast(Instant futureTime) throws InterruptedException { Instant now; ChronoUnit unit = getUnit(); /* * Distributed storage managers that rely on timestamps play with the * least significant bit in timestamp longs, turning it on or off to * ensure that deletions are logically ordered before additions within a * single batch mutation. This is not a problem at microsecond * resolution because we pretend to have microsecond resolution by * multiplying currentTimeMillis by 1000, so the LSB can vary freely. * It's also not a problem with nanosecond resolution because the * resolution is just too fine, relative to how long a mutation takes, * for it to matter in practice. But it can lead to corruption at * millisecond resolution (and does, in testing). */ if (unit.equals(ChronoUnit.MILLIS)) futureTime = futureTime.plusMillis(1L); while ((now = getTime()).compareTo(futureTime) <= 0) { long delta = getTime(futureTime) - getTime(now); if (0L == delta) delta = 1L; if (log.isTraceEnabled()) { log.trace("Sleeping: now={} targettime={} delta={} {}", now, futureTime, delta, unit); } Temporals.timeUnit(unit).sleep(delta); } return now; }
private boolean isAfterDelay() { long openedAt = circuitOpenedAt.get(); long delay = config.get(CircuitBreakerConfig.DELAY); if (delay == 0) { return true; } ChronoUnit delayUnit = config.get(CircuitBreakerConfig.DELAY_UNIT); long elapsed; if (delayUnit.equals(ChronoUnit.MILLIS)) { elapsed = System.currentTimeMillis() - openedAt; } else { Instant start = Instant.ofEpochMilli(openedAt); Instant now = Instant.now(); elapsed = delayUnit.between(start, now); } return elapsed >= delay; }
/** * Create an an instance of {@link TimeDelay} with the {@code expectedTime} and the {@code delay} * of a certain {@code unit}. * * @param expectedTime the expected time such as : 1st January 2000 at 12:00 * @param delay delay in the {@code unit} counting from the {@code expectedTime} * @param unit unit of the delay (unsupported units are {@code ChronoUnit.MICROS}, {@code ChronoUnit.NANOS} * and all not supported by {@link LocalTime#isSupported(TemporalUnit)}) * @return a {@link TimeDelay} with the {@code expectedTime} and the {@code delay}, * {@code null} if the {@code expectedTime} is {@code null}. * @throws UnsupportedTemporalTypeException if the unit is not supported */ public static TimeDelay of(LocalTime expectedTime, Long delay, TemporalUnit unit) { TimeDelay result = null; Objects.requireNonNull(unit); if (expectedTime != null) { Duration duration = Duration.of(DEFAULT_DELAY, DELAY_UNIT); if (delay != null) { if (!unit.isSupportedBy(expectedTime) || ChronoUnit.NANOS.equals(unit) || ChronoUnit.MICROS.equals(unit)) { throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } duration = Duration.of(delay, unit); } result = new TimeDelay(expectedTime, duration.toMillis()); } return result; }
@Override public Instant sleepPast(Instant futureTime) throws InterruptedException { Instant now; ChronoUnit unit = getUnit(); /* * Distributed storage managers that rely on timestamps play with the * least significant bit in timestamp longs, turning it on or off to * ensure that deletions are logically ordered before additions within a * single batch mutation. This is not a problem at microsecond * resolution because we pretendulate microsecond resolution by * multiplying currentTimeMillis by 1000, so the LSB can vary freely. * It's also not a problem with nanosecond resolution because the * resolution is just too fine, relative to how long a mutation takes, * for it to matter in practice. But it can lead to corruption at * millisecond resolution (and does, in testing). */ if (unit.equals(TimeUnit.MILLISECONDS)) futureTime = futureTime.plusMillis(1L); while ((now = getTime()).compareTo(futureTime) <= 0) { long delta = getTime(futureTime) - getTime(now); if (0L == delta) delta = 1L; if (log.isTraceEnabled()) { log.trace("Sleeping: now={} targettime={} delta={} {}", new Object[] { now, futureTime, delta, unit }); } Temporals.timeUnit(unit).sleep(delta); } return now; }