Class RateLimiterImpl<R>

  • Type Parameters:
    R - result type
    All Implemented Interfaces:
    Policy<R>, RateLimiter<R>

    public class RateLimiterImpl<R>
    extends java.lang.Object
    implements RateLimiter<R>
    A RateLimiter implementation that supports smooth and bursty rate limiting.
    • Method Detail

      • acquirePermits

        public void acquirePermits​(int permits)
                            throws java.lang.InterruptedException
        Description copied from interface: RateLimiter
        Attempts to acquire the requested permits to perform executions against the rate limiter, waiting until they are available or the thread is interrupted.
        Specified by:
        acquirePermits in interface RateLimiter<R>
        Throws:
        java.lang.InterruptedException - if the current thread is interrupted while waiting to acquire the permits
        See Also:
        RateLimiter.tryAcquirePermits(int), RateLimiter.reservePermits(int)
      • tryAcquirePermits

        public boolean tryAcquirePermits​(int permits)
        Description copied from interface: RateLimiter
        Tries to acquire the requested permits to perform executions against the rate limiter, returning immediately without waiting.
        Specified by:
        tryAcquirePermits in interface RateLimiter<R>
        Returns:
        whether the requested permits are successfully acquired or not
        See Also:
        RateLimiter.acquirePermits(int)
      • tryAcquirePermits

        public boolean tryAcquirePermits​(int permits,
                                         java.time.Duration maxWaitTime)
                                  throws java.lang.InterruptedException
        Description copied from interface: RateLimiter
        Tries to acquire the requested permits to perform executions against the rate limiter, waiting up to the maxWaitTime until they are available.
        Specified by:
        tryAcquirePermits in interface RateLimiter<R>
        Returns:
        whether the requested permits are successfully acquired or not
        Throws:
        java.lang.InterruptedException - if the current thread is interrupted while waiting to acquire the permits
        See Also:
        RateLimiter.acquirePermits(int, Duration)
      • tryReservePermits

        public java.time.Duration tryReservePermits​(int permits,
                                                    java.time.Duration maxWaitTime)
        Description copied from interface: RateLimiter
        Tries to reserve the permits to perform executions against the rate limiter, and returns the time that the caller is expected to wait before acting on the permits, as long as it's less than the maxWaitTime.
        • Returns the expected wait time for the permits if they were successfully reserved.
        • Returns 0 if the permits were successfully reserved and no waiting is needed.
        • Returns -1 nanoseconds if the permits were not reserved because the wait time would be greater than the maxWaitTime.
        Specified by:
        tryReservePermits in interface RateLimiter<R>
        See Also:
        RateLimiter.acquirePermit(Duration), RateLimiter.tryAcquirePermit(Duration)
      • reservePermits

        long reservePermits​(int permits,
                            java.time.Duration maxWaitTime)