Class RateLimiter

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class RateLimiter
    extends java.lang.Object
    implements java.lang.AutoCloseable
    A Rate Limiter that distributes permits at a configurable rate. Each acquire() blocks if necessary until a permit is available, and then takes it. Each tryAcquire() tries to acquire permits from available permits, it returns true if it succeed else returns false. Rate limiter release configured permits at every configured rate time, so, on next ticket new fresh permits will be available.

    For example: if RateLimiter is configured to release 10 permits at every 1 second then RateLimiter will allow to acquire 10 permits at any time with in that 1 second.

    Comparison with other RateLimiter such as RateLimiter

    • Per second rate-limiting: Per second rate-limiting not satisfied by Guava-RateLimiter
    • Guava RateLimiter: For X permits: it releases X/1000 permits every msec. therefore, for permits=2/sec => it release 1st permit on first 500msec and 2nd permit on next 500ms. therefore, if 2 request comes with in 500msec duration then 2nd request fails to acquire permit though we have configured 2 permits/second.
    • RateLimiter: it releases X permits every second. so, in above usecase: if 2 requests comes at the same time then both will acquire the permit.
    • Faster: RateLimiter is light-weight and faster than Guava-RateLimiter
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void acquire()
      Acquires the given number of permits from this RateLimiter, blocking until the request be granted.
      void acquire​(long acquirePermit)
      Acquires the given number of permits from this RateLimiter, blocking until the request be granted.
      void close()  
      protected java.util.concurrent.ScheduledFuture<?> createTask()  
      long getAvailablePermits()
      Return available permits for this RateLimiter.
      long getRate()
      Returns configured permit rate per pre-configured rate-period.
      long getRateTime()  
      java.util.concurrent.TimeUnit getRateTimeUnit()  
      boolean isClosed()  
      void setRate​(long permits)
      Resets new rate by configuring new value for permits per configured rate-period.
      void setRate​(long permits, long rateTime, java.util.concurrent.TimeUnit timeUnit, java.util.function.Supplier<java.lang.Long> permitUpdaterByte)
      Resets new rate with new permits and rate-time.
      java.lang.String toString()  
      boolean tryAcquire()
      Acquires permits from this RateLimiter if it can be acquired immediately without delay.
      boolean tryAcquire​(long acquirePermit)
      Acquires permits from this RateLimiter if it can be acquired immediately without delay.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
      • isClosed

        public boolean isClosed()
      • acquire

        public void acquire()
                     throws java.lang.InterruptedException
        Acquires the given number of permits from this RateLimiter, blocking until the request be granted.

        This method is equivalent to acquire(1).

        Throws:
        java.lang.InterruptedException
      • acquire

        public void acquire​(long acquirePermit)
                     throws java.lang.InterruptedException
        Acquires the given number of permits from this RateLimiter, blocking until the request be granted.
        Parameters:
        acquirePermit - the number of permits to acquire
        Throws:
        java.lang.InterruptedException
      • tryAcquire

        public boolean tryAcquire()
        Acquires permits from this RateLimiter if it can be acquired immediately without delay.

        This method is equivalent to tryAcquire(1).

        Returns:
        true if the permits were acquired, false otherwise
      • tryAcquire

        public boolean tryAcquire​(long acquirePermit)
        Acquires permits from this RateLimiter if it can be acquired immediately without delay.
        Parameters:
        acquirePermit - the number of permits to acquire
        Returns:
        true if the permits were acquired, false otherwise
      • getAvailablePermits

        public long getAvailablePermits()
        Return available permits for this RateLimiter.
        Returns:
        returns 0 if permits is not available
      • setRate

        public void setRate​(long permits)
        Resets new rate by configuring new value for permits per configured rate-period.
        Parameters:
        permits -
      • setRate

        public void setRate​(long permits,
                            long rateTime,
                            java.util.concurrent.TimeUnit timeUnit,
                            java.util.function.Supplier<java.lang.Long> permitUpdaterByte)
        Resets new rate with new permits and rate-time.
        Parameters:
        permits -
        rateTime -
        timeUnit -
        permitUpdaterByte -
      • getRate

        public long getRate()
        Returns configured permit rate per pre-configured rate-period.
        Returns:
        rate
      • getRateTime

        public long getRateTime()
      • getRateTimeUnit

        public java.util.concurrent.TimeUnit getRateTimeUnit()
      • createTask

        protected java.util.concurrent.ScheduledFuture<?> createTask()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object