
org.rnorth.ducttape.ratelimits.ConstantThroughputRateLimiter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of duct-tape Show documentation
Show all versions of duct-tape Show documentation
General purpose resilience utilities for Java 8 (circuit breakers, timeouts, rate limiters, and handlers for unreliable or inconsistent results)
The newest version!
package org.rnorth.ducttape.ratelimits;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.TimeUnit;
/**
* A rate limiter that uses a simple 'run every n millis' strategy to achieve constant throughput.
*/
class ConstantThroughputRateLimiter extends RateLimiter {
private final long timeBetweenInvocations;
ConstantThroughputRateLimiter(@NotNull Integer rate, @NotNull TimeUnit perTimeUnit) {
this.timeBetweenInvocations = perTimeUnit.toMillis(1) / rate;
}
@Override
protected long getWaitBeforeNextInvocation() {
long timeToNextAllowed = (lastInvocation + timeBetweenInvocations) - System.currentTimeMillis();
// Clamp wait time to 0<
return Math.max(timeToNextAllowed, 0);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy