public class SemaphoreBasedRateLimiter extends java.lang.Object implements RateLimiter
Semaphore and scheduler that will refresh
permissions after each RateLimiterConfig.getLimitRefreshPeriod().RateLimiter.EventPublisher, RateLimiter.Metrics| Constructor and Description |
|---|
SemaphoreBasedRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig)
Creates a RateLimiter.
|
SemaphoreBasedRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig,
io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
Creates a RateLimiter.
|
SemaphoreBasedRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig,
java.util.concurrent.ScheduledExecutorService scheduler)
Creates a RateLimiter.
|
SemaphoreBasedRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig,
java.util.concurrent.ScheduledExecutorService scheduler,
io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
Creates a RateLimiter.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
acquirePermission(int permits)
Acquires the given number of permits from this rate limiter, blocking until one is available,
or the thread is interrupted.
|
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
|
RateLimiterConfig |
getRateLimiterConfig()
Get the RateLimiterConfig of this RateLimiter.
|
io.vavr.collection.Map<java.lang.String,java.lang.String> |
getTags()
Returns an unmodifiable map with tags assigned to this RateLimiter.
|
long |
reservePermission()
Reserving permissions is not supported in the semaphore based implementation.
|
long |
reservePermission(int permits)
Reserves the given number permits 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, waitacquirePermission, decorateCallable, decorateCallable, decorateCheckedFunction, decorateCheckedFunction, decorateCheckedFunction, decorateCheckedRunnable, decorateCheckedRunnable, decorateCheckedSupplier, decorateCheckedSupplier, decorateCompletionStage, decorateCompletionStage, decorateConsumer, decorateConsumer, decorateConsumer, decorateEitherSupplier, decorateEitherSupplier, decorateFunction, decorateFunction, decorateFunction, decorateFuture, decorateFuture, decorateRunnable, decorateRunnable, decorateSupplier, decorateSupplier, decorateTrySupplier, decorateTrySupplier, executeCallable, executeCallable, executeCheckedSupplier, executeCheckedSupplier, executeCompletionStage, executeEitherSupplier, executeEitherSupplier, executeRunnable, executeRunnable, executeSupplier, executeSupplier, executeTrySupplier, executeTrySupplier, of, of, of, of, ofDefaults, waitForPermission, 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,
io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
name - the name of the RateLimiterrateLimiterConfig - The RateLimiter configuration.tags - tags to assign to the RateLimiterpublic SemaphoreBasedRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig,
@Nullable
java.util.concurrent.ScheduledExecutorService scheduler)
name - the name of the RateLimiterrateLimiterConfig - The RateLimiter configuration.scheduler - executor that will refresh permissionspublic SemaphoreBasedRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig,
@Nullable
java.util.concurrent.ScheduledExecutorService scheduler,
io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
name - the name of the RateLimiterrateLimiterConfig - The RateLimiter configuration.scheduler - executor that will refresh permissionstags - tags to assign to the RateLimiterpublic 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 acquirePermission(int permits)
RateLimiterConfig.getTimeoutDuration()
If the current thread is interrupted while waiting for a permit then it won't throw InterruptedException, but its interrupt status will be set.
acquirePermission in interface RateLimiterpermits - number of permits - use for systems where 1 call != 1 permittrue if a permit was acquired and false if waiting timeoutDuration
elapsed before a permit was acquiredpublic long reservePermission()
RateLimiter.acquirePermission()reservePermission in interface RateLimiterlong amount of nanoseconds you should wait for reserved permissions. if
negative, it means you failed to reserve.java.lang.UnsupportedOperationException - always for this implementationpublic long reservePermission(int permits)
RateLimiterRateLimiterConfig.getTimeoutDuration() is less then time to wait for
permission.reservePermission in interface RateLimiterpermits - number of permits - use for systems where 1 call != 1 permitlong amount of nanoseconds you should wait for reserved permissions. if
negative, it means you failed to reserve.java.lang.UnsupportedOperationException - always for this implementationreservePermission()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.Objectpublic io.vavr.collection.Map<java.lang.String,java.lang.String> getTags()
RateLimitergetTags in interface RateLimiter