public class SemaphoreBasedRateLimiter extends java.lang.Object implements RateLimiter
Semaphore
and scheduler that will refresh permissions
after each RateLimiterConfig.limitRefreshPeriod.RateLimiter.EventPublisher, RateLimiter.Metrics| Constructor and Description |
|---|
SemaphoreBasedRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig)
Creates a RateLimiter.
|
SemaphoreBasedRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig,
java.util.concurrent.ScheduledExecutorService scheduler)
Creates a RateLimiter.
|
| Modifier and Type | Method and Description |
|---|---|
void |
changeLimitForPeriod(int limitForPeriod)
Dynamic rate limiter configuration change.
|
void |
changeTimeoutDuration(java.time.Duration timeoutDuration)
Dynamic rate limiter configuration change.
|
RateLimiter.EventPublisher |
getEventPublisher()
Returns an EventPublisher which can be used to register event consumers.
|
RateLimiter.Metrics |
getMetrics()
Get the Metrics of this RateLimiter.
|
java.lang.String |
getName()
Get the name of this RateLimiter
|
boolean |
getPermission(java.time.Duration timeoutDuration)
Acquires a permission from this rate limiter, blocking until one is
available.
|
RateLimiterConfig |
getRateLimiterConfig()
Get the RateLimiterConfig of this RateLimiter.
|
long |
reservePermission(java.time.Duration timeoutDuration)
Reserves a permission from this rate limiter and returns nanoseconds you should wait for it.
|
java.lang.String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitdecorateCallable, decorateCheckedFunction, decorateCheckedRunnable, decorateCheckedSupplier, decorateCompletionStage, decorateConsumer, decorateFunction, decorateRunnable, decorateSupplier, executeCallable, executeRunnable, executeSupplier, of, of, ofDefaults, waitForPermissionpublic SemaphoreBasedRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig)
name - the name of the RateLimiterrateLimiterConfig - The RateLimiter configuration.public SemaphoreBasedRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig,
java.util.concurrent.ScheduledExecutorService scheduler)
name - the name of the RateLimiterrateLimiterConfig - The RateLimiter configuration.scheduler - executor that will refresh permissionspublic void changeTimeoutDuration(java.time.Duration timeoutDuration)
changeTimeoutDuration in interface RateLimitertimeoutDuration - new timeout durationpublic void changeLimitForPeriod(int limitForPeriod)
changeLimitForPeriod in interface RateLimiterlimitForPeriod - new permissions limitpublic boolean getPermission(java.time.Duration timeoutDuration)
If the current thread is interrupted while waiting for a permit then it won't throw InterruptedException, but its interrupt status will be set.
getPermission in interface RateLimitertimeoutDuration - the maximum time to waittrue if a permit was acquired and false
if waiting timeoutDuration elapsed before a permit was acquiredpublic long reservePermission(java.time.Duration timeoutDuration)
timeoutDuration is less then time to wait for permission.
SemaphoreBasedRateLimiter is totally blocking by it's nature. So this non-blocking API isn't supported.
It will return negative numbers all the time.reservePermission in interface RateLimitertimeoutDuration - the maximum time you want to wait.long amount of nanoseconds you should wait for reserved permission. if negative, it means you failed to reserve.public java.lang.String getName()
getName in interface RateLimiterpublic RateLimiter.Metrics getMetrics()
getMetrics in interface RateLimiterpublic RateLimiter.EventPublisher getEventPublisher()
RateLimitergetEventPublisher in interface RateLimiterpublic RateLimiterConfig getRateLimiterConfig()
getRateLimiterConfig in interface RateLimiterpublic java.lang.String toString()
toString in class java.lang.Object