@Test public void reserveManyCyclesIfWegithgreaterThenLimitPerPeriod() throws Exception { setup(Duration.ofNanos(CYCLE_IN_NANOS * 5)); setTimeOnNanos(CYCLE_IN_NANOS); long nanosToWait = rateLimiter.reservePermission(PERMISSIONS_RER_CYCLE * 3); then(nanosToWait).isGreaterThan(CYCLE_IN_NANOS); }
@Override public long reservePermission(final int permits) { long timeoutInNanos = state.get().config.getTimeoutDuration().toNanos(); State modifiedState = updateStateWithBackOff(permits, timeoutInNanos); boolean canAcquireImmediately = modifiedState.nanosToWait <= 0; if (canAcquireImmediately) { publishRateLimiterEvent(true, permits); return 0; } boolean canAcquireInTime = timeoutInNanos >= modifiedState.nanosToWait; if (canAcquireInTime) { publishRateLimiterEvent(true, permits); return modifiedState.nanosToWait; } publishRateLimiterEvent(false, permits); return -1; }
AtomicRateLimiter implements RateLimiter { @Override public long reservePermission(final int permits) { long timeoutInNanos = state.get().config.getTimeoutDuration().toNanos(); State modifiedState = updateStateWithBackOff(permits, timeoutInNanos); boolean canAcquireImmediately = modifiedState.nanosToWait <= 0; if (canAcquireImmediately) { publishRateLimiterEvent(true, permits); return 0; } boolean canAcquireInTime = timeoutInNanos >= modifiedState.nanosToWait; if (canAcquireInTime) { publishRateLimiterEvent(true, permits); return modifiedState.nanosToWait; } publishRateLimiterEvent(false, permits); return -1; } }
AtomicRateLimiter implements RateLimiter { @Override public long reservePermission(final int permits) { long timeoutInNanos = state.get().config.getTimeoutDuration().toNanos(); State modifiedState = updateStateWithBackOff(permits, timeoutInNanos); boolean canAcquireImmediately = modifiedState.nanosToWait <= 0; if (canAcquireImmediately) { publishRateLimiterEvent(true, permits); return 0; } boolean canAcquireInTime = timeoutInNanos >= modifiedState.nanosToWait; if (canAcquireInTime) { publishRateLimiterEvent(true, permits); return modifiedState.nanosToWait; } publishRateLimiterEvent(false, permits); return -1; } AtomicRateLimiter(String name, RateLimiterConfig rateLimiterConfig);  AtomicRateLimiter(String name, RateLimiterConfig rateLimiterConfig,
                             Map<String, String> tags); }
AtomicRateLimiter implements RateLimiter { @Override public long reservePermission(final int permits) { long timeoutInNanos = state.get().config.getTimeoutDuration().toNanos(); State modifiedState = updateStateWithBackOff(permits, timeoutInNanos); boolean canAcquireImmediately = modifiedState.nanosToWait <= 0; if (canAcquireImmediately) { publishRateLimiterEvent(true, permits); return 0; } boolean canAcquireInTime = timeoutInNanos >= modifiedState.nanosToWait; if (canAcquireInTime) { publishRateLimiterEvent(true, permits); return modifiedState.nanosToWait; } publishRateLimiterEvent(false, permits); return -1; } AtomicRateLimiter(String name, RateLimiterConfig rateLimiterConfig);  AtomicRateLimiter(String name, RateLimiterConfig rateLimiterConfig,
                             Map<String, String> tags); @Override void changeTimeoutDuration(final Duration timeoutDuration); @Override void changeLimitForPeriod(final int limitForPeriod); @Override boolean acquirePermission(final int permits); @Override long reservePermission(final int permits); @Override String getName(); @Override RateLimiterConfig getRateLimiterConfig(); @Override Map<String, String> getTags(); @Override Metrics getMetrics(); @Override EventPublisher getEventPublisher(); @Override String toString(); AtomicRateLimiterMetrics getDetailedMetrics(); }
AtomicRateLimiter implements RateLimiter { @Override public long reservePermission(final int permits) { long timeoutInNanos = state.get().config.getTimeoutDuration().toNanos(); State modifiedState = updateStateWithBackOff(permits, timeoutInNanos); boolean canAcquireImmediately = modifiedState.nanosToWait <= 0; if (canAcquireImmediately) { publishRateLimiterEvent(true, permits); return 0; } boolean canAcquireInTime = timeoutInNanos >= modifiedState.nanosToWait; if (canAcquireInTime) { publishRateLimiterEvent(true, permits); return modifiedState.nanosToWait; } publishRateLimiterEvent(false, permits); return -1; } AtomicRateLimiter(String name, RateLimiterConfig rateLimiterConfig);  AtomicRateLimiter(String name, RateLimiterConfig rateLimiterConfig,
                             Map<String, String> tags); @Override void changeTimeoutDuration(final Duration timeoutDuration); @Override void changeLimitForPeriod(final int limitForPeriod); @Override boolean acquirePermission(final int permits); @Override long reservePermission(final int permits); @Override String getName(); @Override RateLimiterConfig getRateLimiterConfig(); @Override Map<String, String> getTags(); @Override Metrics getMetrics(); @Override EventPublisher getEventPublisher(); @Override String toString(); AtomicRateLimiterMetrics getDetailedMetrics();  }