public class SingleRateLimiter extends java.lang.Object implements RateLimiter
RateLimiter.Type
Constructor and Description |
---|
SingleRateLimiter(RateLimiter.Type type,
int callsPerEpoch,
long millisPerEpoch) |
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 SingleRateLimiter(RateLimiter.Type type, int callsPerEpoch, long millisPerEpoch)
type
- ROLLING or WINDOW. ROLLING allows one call every
(millisPerEpoch/callsPerEpoch)ms. WINDOW allows
(callsPerEpoch) calls in each (millisPerEpoch)ms stretch.callsPerEpoch
- the number of calls that can go through each epochmillisPerEpoch
- the length of an epochpublic <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