public class RateLimiter extends Object implements AutoCloseable
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
| Constructor and Description |
|---|
RateLimiter(long permits,
long rateTime,
TimeUnit timeUnit) |
RateLimiter(long permits,
long rateTime,
TimeUnit timeUnit,
RateLimitFunction autoReadResetFunction) |
RateLimiter(ScheduledExecutorService service,
long permits,
long rateTime,
TimeUnit timeUnit,
Supplier<Long> permitUpdater) |
RateLimiter(ScheduledExecutorService service,
long permits,
long rateTime,
TimeUnit timeUnit,
Supplier<Long> permitUpdater,
boolean isDispatchRateLimiter) |
| Modifier and Type | Method and 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 ScheduledFuture<?> |
createTask() |
long |
getAvailablePermits()
Return available permits for this
RateLimiter. |
long |
getRate()
Returns configured permit rate per pre-configured rate-period.
|
long |
getRateTime() |
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,
TimeUnit timeUnit,
Supplier<Long> permitUpdaterByte)
Resets new rate with new permits and rate-time.
|
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. |
public RateLimiter(long permits,
long rateTime,
TimeUnit timeUnit)
public RateLimiter(long permits,
long rateTime,
TimeUnit timeUnit,
RateLimitFunction autoReadResetFunction)
public RateLimiter(ScheduledExecutorService service, long permits, long rateTime, TimeUnit timeUnit, Supplier<Long> permitUpdater)
public RateLimiter(ScheduledExecutorService service, long permits, long rateTime, TimeUnit timeUnit, Supplier<Long> permitUpdater, boolean isDispatchRateLimiter)
public void close()
close in interface AutoCloseablepublic boolean isClosed()
public void acquire()
throws InterruptedException
RateLimiter, blocking until the request be granted.
This method is equivalent to acquire(1).
InterruptedExceptionpublic void acquire(long acquirePermit)
throws InterruptedException
RateLimiter, blocking until the request be granted.acquirePermit - the number of permits to acquireInterruptedExceptionpublic boolean tryAcquire()
RateLimiter if it can be acquired immediately without delay.
This method is equivalent to tryAcquire(1).
true if the permits were acquired, false otherwisepublic boolean tryAcquire(long acquirePermit)
RateLimiter if it can be acquired immediately without delay.acquirePermit - the number of permits to acquiretrue if the permits were acquired, false otherwisepublic long getAvailablePermits()
RateLimiter.public void setRate(long permits)
permits - public void setRate(long permits,
long rateTime,
TimeUnit timeUnit,
Supplier<Long> permitUpdaterByte)
permits - rateTime - timeUnit - permitUpdaterByte - public long getRate()
public long getRateTime()
public TimeUnit getRateTimeUnit()
protected ScheduledFuture<?> createTask()
Copyright © 2017–2021 Apache Software Foundation. All rights reserved.