@Test public void rejectedByTimeoutNonBlocking() throws Exception { setup(Duration.ZERO); setTimeOnNanos(CYCLE_IN_NANOS); long permission = rateLimiter.reservePermission(); then(permission).isZero(); then(metrics.getAvailablePermissions()).isEqualTo(0); then(metrics.getNanosToWait()).isEqualTo(CYCLE_IN_NANOS); then(metrics.getNumberOfWaitingThreads()).isEqualTo(0); long failedPermission = rateLimiter.reservePermission(); then(failedPermission).isNegative(); then(metrics.getAvailablePermissions()).isEqualTo(0); then(metrics.getNanosToWait()).isEqualTo(CYCLE_IN_NANOS); then(metrics.getNumberOfWaitingThreads()).isEqualTo(0); setTimeOnNanos(CYCLE_IN_NANOS * 2 - 1); then(metrics.getAvailablePermissions()).isEqualTo(0); then(metrics.getNanosToWait()).isEqualTo(1L); then(metrics.getNumberOfWaitingThreads()).isEqualTo(0); }
@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();  }