public class MultiRateLimiter extends java.lang.Object implements RateLimiter
RateLimiter.Type
Constructor and Description |
---|
MultiRateLimiter(RateLimiter.Type type,
java.util.List<Pair<java.lang.Integer,java.lang.Long>> limits) |
Modifier and Type | Method and Description |
---|---|
<T> T |
attemptCall(java.util.function.Supplier<T> call)
Executes the call only if calls are available
|
long |
millisUntilNextCall() |
int |
numCallsLeft() |
<T> T |
waitForCall(java.util.function.Supplier<T> call)
Executes the call, waiting for more calls if necessary
|
public MultiRateLimiter(RateLimiter.Type type, java.util.List<Pair<java.lang.Integer,java.lang.Long>> limits)
type
- ROLLING or WINDOW. ROLLING allows one call every
(millisPerEpoch/callsPerEpoch)ms. WINDOW allows
(callsPerEpoch) calls in each (millisPerEpoch)ms stretch.limits
- pairs of the number of calls per epoch and the millisecond
length of an epoch. Each pair is an addition restriction.public <T> T attemptCall(java.util.function.Supplier<T> call)
RateLimiter
attemptCall
in interface RateLimiter
T
- the resulting value of the callcall
- the call to executepublic long millisUntilNextCall()
millisUntilNextCall
in interface RateLimiter
public int numCallsLeft()
numCallsLeft
in interface RateLimiter
public <T> T waitForCall(java.util.function.Supplier<T> call) throws java.lang.InterruptedException
RateLimiter
waitForCall
in interface RateLimiter
T
- the resulting value of the callcall
- the call to executejava.lang.InterruptedException
- if the waiting is interrupted