Class TokenBucketThrottlingStrategy
- java.lang.Object
-
- org.forgerock.openig.filter.throttling.TokenBucketThrottlingStrategy
-
- All Implemented Interfaces:
ThrottlingStrategy
public class TokenBucketThrottlingStrategy extends Object implements ThrottlingStrategy
The rate limiting is implemented as a token bucket strategy that gives us the ability to handle rate limits through a sliding window. Multiple rates can be supported in parallel with the support of a partition key (we first try to find the bucket to use for each incoming request, then we apply the rate limit).
-
-
Constructor Summary
Constructors Constructor Description TokenBucketThrottlingStrategy(com.google.common.base.Ticker ticker, ScheduledExecutorService scheduledExecutor, Duration cleaningInterval)
Constructs a newTokenBucketThrottlingStrategy
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
stop()
Stop and free any resources needed by the strategy.Promise<Long,NeverThrowsException>
throttle(String partitionKey, ThrottlingRate throttlingRate)
Based on the given partitionKey and throttlingRate, return if the call is accepted or not.
-
-
-
Constructor Detail
-
TokenBucketThrottlingStrategy
public TokenBucketThrottlingStrategy(com.google.common.base.Ticker ticker, ScheduledExecutorService scheduledExecutor, Duration cleaningInterval)
Constructs a newTokenBucketThrottlingStrategy
.- Parameters:
ticker
- theTicker
to use to follow the timeline.scheduledExecutor
- theScheduledExecutorService
used to schedule cleaning tasks.cleaningInterval
- the interval between 2 cleaning tasks.
-
-
Method Detail
-
throttle
public Promise<Long,NeverThrowsException> throttle(String partitionKey, ThrottlingRate throttlingRate)
Description copied from interface:ThrottlingStrategy
Based on the given partitionKey and throttlingRate, return if the call is accepted or not. The returned promise is succeeded with a value of 0 if the call is accepted. Any value greater than 0 means that the next call that could be accepted after value nanoseconds.This method returns a promise as the decision to let this call going through can be queued and thus completed later. That may allow for example to queue the first calls and complete the returned promises at a constant rate.
- Specified by:
throttle
in interfaceThrottlingStrategy
- Parameters:
partitionKey
- the key used to identify the different groupsthrottlingRate
- the throttling rate to apply- Returns:
- a
Promise
meaning that the call is accepted (succeeds with 0) or refused (succeeds with value greater than 0)
-
stop
public void stop()
Description copied from interface:ThrottlingStrategy
Stop and free any resources needed by the strategy.- Specified by:
stop
in interfaceThrottlingStrategy
-
-