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 Details

    • TokenBucketThrottlingStrategy

      public TokenBucketThrottlingStrategy(com.google.common.base.Ticker ticker, ScheduledExecutorService scheduledExecutor, Duration cleaningInterval)
      Parameters:
      ticker - the Ticker to use to follow the timeline.
      scheduledExecutor - the ScheduledExecutorService used 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: 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 interface ThrottlingStrategy
      Parameters:
      partitionKey - the key used to identify the different groups
      throttlingRate - 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 interface ThrottlingStrategy