public class AtomicRateLimiter extends java.lang.Object implements RateLimiter
AtomicRateLimiter splits all nanoseconds from the start of epoch into cycles.
Each cycle has duration of RateLimiterConfig.limitRefreshPeriod in nanoseconds.
By contract on start of each cycle AtomicRateLimiter should
set State#activePermissions to RateLimiterConfig.limitForPeriod. For the AtomicRateLimiter callers it is really looks so, but under the hood there is some optimisations
that will skip this refresh if AtomicRateLimiter is not used actively.
All AtomicRateLimiter updates are atomic and state is encapsulated in AtomicReference to AtomicRateLimiter.State
| Modifier and Type | Class and Description |
|---|---|
class |
AtomicRateLimiter.AtomicRateLimiterMetrics
Enhanced
Metrics with some implementation specific details |
RateLimiter.EventPublisher, RateLimiter.Metrics| Constructor and Description |
|---|
AtomicRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig) |
AtomicRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig,
io.vavr.collection.Map<java.lang.String,java.lang.String> tags) |
| 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.
|
AtomicRateLimiter.AtomicRateLimiterMetrics |
getDetailedMetrics()
Get the enhanced Metrics with some implementation specific details.
|
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(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, reservePermission, waitForPermission, waitForPermissionpublic AtomicRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig)
public AtomicRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig,
io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
public 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(int permits)
RateLimiterConfig.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.public java.lang.String getName()
getName in interface RateLimiterpublic RateLimiterConfig getRateLimiterConfig()
getRateLimiterConfig in interface RateLimiterpublic io.vavr.collection.Map<java.lang.String,java.lang.String> getTags()
getTags in interface RateLimiterpublic RateLimiter.Metrics getMetrics()
getMetrics in interface RateLimiterpublic RateLimiter.EventPublisher getEventPublisher()
RateLimitergetEventPublisher in interface RateLimiterpublic java.lang.String toString()
toString in class java.lang.Objectpublic AtomicRateLimiter.AtomicRateLimiterMetrics getDetailedMetrics()