Class AtomicRateLimiter
java.lang.Object
io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter
- All Implemented Interfaces:
RateLimiter
AtomicRateLimiter
splits all nanoseconds from the start of epoch into cycles.
Each cycle has duration of RateLimiterConfig.getLimitRefreshPeriod()
in nanoseconds.
By contract on start of each cycle AtomicRateLimiter
should
set AtomicRateLimiter.State.activePermissions
to RateLimiterConfig.getLimitForPeriod()
. 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
-
Nested Class Summary
Modifier and TypeClassDescriptionclass
EnhancedRateLimiter.Metrics
with some implementation specific detailsNested classes/interfaces inherited from interface io.github.resilience4j.ratelimiter.RateLimiter
RateLimiter.EventPublisher, RateLimiter.Metrics
-
Constructor Summary
ConstructorDescriptionAtomicRateLimiter
(String name, RateLimiterConfig rateLimiterConfig) AtomicRateLimiter
(String name, RateLimiterConfig rateLimiterConfig, Map<String, String> tags) -
Method Summary
Modifier and TypeMethodDescriptionboolean
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
(Duration timeoutDuration) Dynamic rate limiter configuration change.void
Drains all the permits left in the current period.Get the enhanced Metrics with some implementation specific details.Returns an EventPublisher which can be used to register event consumers.Get the Metrics of this RateLimiter.getName()
Get the name of this RateLimiterGet the RateLimiterConfig of this RateLimiter.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.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface io.github.resilience4j.ratelimiter.RateLimiter
acquirePermission, drainIfNeeded, executeCallable, executeCallable, executeCheckedSupplier, executeCheckedSupplier, executeCompletionStage, executeRunnable, executeRunnable, executeSupplier, executeSupplier, onError, onResult, onSuccess, reservePermission
-
Constructor Details
-
AtomicRateLimiter
-
AtomicRateLimiter
-
-
Method Details
-
changeTimeoutDuration
Dynamic rate limiter configuration change. This method allows to change timeout duration of current limiter. NOTE! New timeout duration won't affect threads that are currently waiting for permission.- Specified by:
changeTimeoutDuration
in interfaceRateLimiter
- Parameters:
timeoutDuration
- new timeout duration
-
changeLimitForPeriod
public void changeLimitForPeriod(int limitForPeriod) Dynamic rate limiter configuration change. This method allows to change count of permissions available during refresh period. NOTE! New limit won't affect current period permissions and will apply only from next one.- Specified by:
changeLimitForPeriod
in interfaceRateLimiter
- Parameters:
limitForPeriod
- new permissions limit
-
acquirePermission
public boolean acquirePermission(int permits) Acquires the given number of permits from this rate limiter, blocking until one is available, or the thread is interrupted. Maximum wait time isRateLimiterConfig.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.
- Specified by:
acquirePermission
in interfaceRateLimiter
- Parameters:
permits
- number of permits - use for systems where 1 call != 1 permit- Returns:
true
if a permit was acquired andfalse
if waiting timeoutDuration elapsed before a permit was acquired
-
reservePermission
public long reservePermission(int permits) Reserves the given number permits from this rate limiter and returns nanoseconds you should wait for it. If returned long is negative, it means that you failed to reserve permission, possibly yourRateLimiterConfig.getTimeoutDuration()
is less then time to wait for permission.- Specified by:
reservePermission
in interfaceRateLimiter
- Parameters:
permits
- number of permits - use for systems where 1 call != 1 permit- Returns:
long
amount of nanoseconds you should wait for reserved permissions. if negative, it means you failed to reserve.
-
drainPermissions
public void drainPermissions()Description copied from interface:RateLimiter
Drains all the permits left in the current period.- Specified by:
drainPermissions
in interfaceRateLimiter
-
getName
Get the name of this RateLimiter- Specified by:
getName
in interfaceRateLimiter
- Returns:
- the name of this RateLimiter
-
getRateLimiterConfig
Get the RateLimiterConfig of this RateLimiter.- Specified by:
getRateLimiterConfig
in interfaceRateLimiter
- Returns:
- the RateLimiterConfig of this RateLimiter
-
getTags
Returns an unmodifiable map with tags assigned to this RateLimiter.- Specified by:
getTags
in interfaceRateLimiter
- Returns:
- the tags assigned to this Retry in an unmodifiable map
-
getMetrics
Get the Metrics of this RateLimiter.- Specified by:
getMetrics
in interfaceRateLimiter
- Returns:
- the Metrics of this RateLimiter
-
getEventPublisher
Description copied from interface:RateLimiter
Returns an EventPublisher which can be used to register event consumers.- Specified by:
getEventPublisher
in interfaceRateLimiter
- Returns:
- an EventPublisher
-
toString
-
getDetailedMetrics
Get the enhanced Metrics with some implementation specific details.- Returns:
- the detailed metrics
-