Class 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 Detail

      • 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 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 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)