Class TokenBucketThrottlingStrategy
java.lang.Object
org.forgerock.openig.filter.throttling.TokenBucketThrottlingStrategy
- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionTokenBucketThrottlingStrategy(com.google.common.base.Ticker ticker, ScheduledExecutorService scheduledExecutor, Duration cleaningInterval) Constructs a newTokenBucketThrottlingStrategy. -
Method Summary
Modifier and TypeMethodDescriptionvoidstop()Stop and free any resources needed by the strategy.throttle(String partitionKey, ThrottlingRate throttlingRate) Based on the given partitionKey and throttlingRate, return if the call is accepted or not.
-
Constructor Details
-
TokenBucketThrottlingStrategy
public TokenBucketThrottlingStrategy(com.google.common.base.Ticker ticker, ScheduledExecutorService scheduledExecutor, Duration cleaningInterval) Constructs a newTokenBucketThrottlingStrategy.- Parameters:
ticker- theTickerto use to follow the timeline.scheduledExecutor- theScheduledExecutorServiceused to schedule cleaning tasks.cleaningInterval- the interval between 2 cleaning tasks.
-
-
Method Details
-
throttle
public Promise<Long,NeverThrowsException> throttle(String partitionKey, ThrottlingRate throttlingRate) Description copied from interface:ThrottlingStrategyBased 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:
throttlein interfaceThrottlingStrategy- Parameters:
partitionKey- the key used to identify the different groupsthrottlingRate- the throttling rate to apply- Returns:
- a
Promisemeaning that the call is accepted (succeeds with 0) or refused (succeeds with value greater than 0)
-
stop
public void stop()Description copied from interface:ThrottlingStrategyStop and free any resources needed by the strategy.- Specified by:
stopin interfaceThrottlingStrategy
-