Package org.apache.pulsar.common.util
Class RateLimiter
java.lang.Object
org.apache.pulsar.common.util.RateLimiter
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidacquire()Acquires the given number of permits from thisRateLimiter, blocking until the request be granted.voidacquire(long acquirePermit) Acquires the given number of permits from thisRateLimiter, blocking until the request be granted.voidclose()protected ScheduledFuture<?>longReturn available permits for thisRateLimiter.longgetRate()Returns configured permit rate per pre-configured rate-period.longbooleanisClosed()voidsetRate(long permits) Resets new rate by configuring new value for permits per configured rate-period.voidResets new rate with new permits and rate-time.toString()booleanAcquires permits from thisRateLimiterif it can be acquired immediately without delay.booleantryAcquire(long acquirePermit) Acquires permits from thisRateLimiterif it can be acquired immediately without delay.
-
Method Details
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
isClosed
public boolean isClosed() -
acquire
Acquires the given number of permits from thisRateLimiter, blocking until the request be granted.This method is equivalent to
acquire(1).- Throws:
InterruptedException
-
acquire
Acquires the given number of permits from thisRateLimiter, blocking until the request be granted.- Parameters:
acquirePermit- the number of permits to acquire- Throws:
InterruptedException
-
tryAcquire
public boolean tryAcquire()Acquires permits from thisRateLimiterif it can be acquired immediately without delay.This method is equivalent to
tryAcquire(1).- Returns:
trueif the permits were acquired,falseotherwise
-
tryAcquire
public boolean tryAcquire(long acquirePermit) Acquires permits from thisRateLimiterif it can be acquired immediately without delay.- Parameters:
acquirePermit- the number of permits to acquire- Returns:
trueif the permits were acquired,falseotherwise
-
getAvailablePermits
public long getAvailablePermits()Return available permits for thisRateLimiter.- 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
-
createTask
-
toString
-