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) |
| 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.
|
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
|
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 AtomicRateLimiter(java.lang.String name,
RateLimiterConfig rateLimiterConfig)
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 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.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 RateLimiterConfig getRateLimiterConfig()
getRateLimiterConfig 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()