Class AtomicRateLimiter
- java.lang.Object
-
- io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter
-
- All Implemented Interfaces:
RateLimiter
public class AtomicRateLimiter extends java.lang.Object implements RateLimiter
AtomicRateLimitersplits all nanoseconds from the start of epoch into cycles.Each cycle has duration of
RateLimiterConfig.limitRefreshPeriodin nanoseconds.By contract on start of each cycle
AtomicRateLimitershould setAtomicRateLimiter.State.activePermissionstoRateLimiterConfig.limitForPeriod. For theAtomicRateLimitercallers it is really looks so, but under the hood there is some optimisations that will skip this refresh ifAtomicRateLimiteris not used actively.All
AtomicRateLimiterupdates are atomic and state is encapsulated inAtomicReferencetoAtomicRateLimiter.State
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classAtomicRateLimiter.AtomicRateLimiterMetricsEnhancedRateLimiter.Metricswith some implementation specific details-
Nested classes/interfaces inherited from interface io.github.resilience4j.ratelimiter.RateLimiter
RateLimiter.EventPublisher, RateLimiter.Metrics
-
-
Constructor Summary
Constructors Constructor 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)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanacquirePermission(int permits)Acquires the given number of permits from this rate limiter, blocking until one is available, or the thread is interrupted.voidchangeLimitForPeriod(int limitForPeriod)Dynamic rate limiter configuration change.voidchangeTimeoutDuration(java.time.Duration timeoutDuration)Dynamic rate limiter configuration change.AtomicRateLimiter.AtomicRateLimiterMetricsgetDetailedMetrics()Get the enhanced Metrics with some implementation specific details.RateLimiter.EventPublishergetEventPublisher()Returns an EventPublisher which can be used to register event consumers.RateLimiter.MetricsgetMetrics()Get the Metrics of this RateLimiter.java.lang.StringgetName()Get the name of this RateLimiterRateLimiterConfiggetRateLimiterConfig()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.longreservePermission(int permits)Reserves the given number permits from this rate limiter and returns nanoseconds you should wait for it.java.lang.StringtoString()-
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, executeCallable, executeCallable, executeCheckedSupplier, executeCheckedSupplier, executeCompletionStage, executeEitherSupplier, executeEitherSupplier, executeRunnable, executeRunnable, executeSupplier, executeSupplier, executeTrySupplier, executeTrySupplier, reservePermission
-
-
-
-
Constructor Detail
-
AtomicRateLimiter
public AtomicRateLimiter(java.lang.String name, RateLimiterConfig rateLimiterConfig)
-
AtomicRateLimiter
public AtomicRateLimiter(java.lang.String name, RateLimiterConfig rateLimiterConfig, io.vavr.collection.Map<java.lang.String,java.lang.String> tags)
-
-
Method Detail
-
changeTimeoutDuration
public void changeTimeoutDuration(java.time.Duration timeoutDuration)
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:
changeTimeoutDurationin 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:
changeLimitForPeriodin 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:
acquirePermissionin interfaceRateLimiter- Parameters:
permits- number of permits - use for systems where 1 call != 1 permit- Returns:
trueif a permit was acquired andfalseif 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:
reservePermissionin interfaceRateLimiter- Parameters:
permits- number of permits - use for systems where 1 call != 1 permit- Returns:
longamount of nanoseconds you should wait for reserved permissions. if negative, it means you failed to reserve.
-
getName
public java.lang.String getName()
Get the name of this RateLimiter- Specified by:
getNamein interfaceRateLimiter- Returns:
- the name of this RateLimiter
-
getRateLimiterConfig
public RateLimiterConfig getRateLimiterConfig()
Get the RateLimiterConfig of this RateLimiter.- Specified by:
getRateLimiterConfigin interfaceRateLimiter- Returns:
- the RateLimiterConfig of this RateLimiter
-
getTags
public io.vavr.collection.Map<java.lang.String,java.lang.String> getTags()
Returns an unmodifiable map with tags assigned to this RateLimiter.- Specified by:
getTagsin interfaceRateLimiter- Returns:
- the tags assigned to this Retry in an unmodifiable map
-
getMetrics
public RateLimiter.Metrics getMetrics()
Get the Metrics of this RateLimiter.- Specified by:
getMetricsin interfaceRateLimiter- Returns:
- the Metrics of this RateLimiter
-
getEventPublisher
public RateLimiter.EventPublisher getEventPublisher()
Description copied from interface:RateLimiterReturns an EventPublisher which can be used to register event consumers.- Specified by:
getEventPublisherin interfaceRateLimiter- Returns:
- an EventPublisher
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getDetailedMetrics
public AtomicRateLimiter.AtomicRateLimiterMetrics getDetailedMetrics()
Get the enhanced Metrics with some implementation specific details.- Returns:
- the detailed metrics
-
-