Class RateLimiter

java.lang.Object
org.apache.pulsar.common.util.RateLimiter
All Implemented Interfaces:
AutoCloseable

public class RateLimiter extends Object implements 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 Details

    • close

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

      public boolean isClosed()
    • acquire

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

      This method is equivalent to acquire(1).

      Throws:
      InterruptedException
    • acquire

      public void acquire(long acquirePermit) throws 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:
      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, TimeUnit timeUnit, Supplier<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 TimeUnit getRateTimeUnit()
    • createTask

      protected ScheduledFuture<?> createTask()
    • toString

      public String toString()
      Overrides:
      toString in class Object