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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
acquire()
Acquires the given number of permits from thisRateLimiter
, blocking until the request be granted.void
acquire
(long acquirePermit) Acquires the given number of permits from thisRateLimiter
, blocking until the request be granted.void
close()
protected ScheduledFuture<?>
long
Return available permits for thisRateLimiter
.long
getRate()
Returns configured permit rate per pre-configured rate-period.long
boolean
isClosed()
void
setRate
(long permits) Resets new rate by configuring new value for permits per configured rate-period.void
Resets new rate with new permits and rate-time.toString()
boolean
Acquires permits from thisRateLimiter
if it can be acquired immediately without delay.boolean
tryAcquire
(long acquirePermit) Acquires permits from thisRateLimiter
if it can be acquired immediately without delay.
-
Method Details
-
close
public void close()- Specified by:
close
in 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 thisRateLimiter
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 thisRateLimiter
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 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
-