Class SemaphoreBasedRateLimiter
java.lang.Object
io.github.resilience4j.ratelimiter.internal.SemaphoreBasedRateLimiter
- All Implemented Interfaces:
RateLimiter
A RateLimiter implementation that consists of
Semaphore and scheduler that will refresh
permissions after each RateLimiterConfig.getLimitRefreshPeriod(), you can invoke
shutdown() to close the limiter.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final classNested classes/interfaces inherited from interface RateLimiter
RateLimiter.EventPublisher, RateLimiter.Metrics -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Stringprivate final RateLimiterEventProcessorprivate final Stringprivate static final Stringprivate final AtomicReference<RateLimiterConfig> private final ScheduledFuture<?> private final ScheduledExecutorServiceprivate final Semaphore -
Constructor Summary
ConstructorsConstructorDescriptionSemaphoreBasedRateLimiter(String name, RateLimiterConfig rateLimiterConfig) Creates a RateLimiter.SemaphoreBasedRateLimiter(String name, RateLimiterConfig rateLimiterConfig, ScheduledExecutorService scheduler) Creates a RateLimiter.SemaphoreBasedRateLimiter(String name, RateLimiterConfig rateLimiterConfig, ScheduledExecutorService scheduler, Map<String, String> tags) Creates a RateLimiter.SemaphoreBasedRateLimiter(String name, RateLimiterConfig rateLimiterConfig, Map<String, String> tags) Creates a RateLimiter. -
Method Summary
Modifier and TypeMethodDescriptionbooleanacquirePermission(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(Duration timeoutDuration) Dynamic rate limiter configuration change.private ScheduledExecutorServicevoidDrains all the permits left in the current period.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.private voidpublishRateLimiterAcquisitionEvent(boolean permissionAcquired, int permits) (package private) voidlongReserving permissions is not supported in the semaphore based implementation.longreservePermission(int permits) Reserves the given number permits from this rate limiter and returns nanoseconds you should wait for it.private ScheduledFuture<?> voidshutdown()Close the scheduled task that refresh permissions if you don't use theSemaphoreBasedRateLimiteranymore.toString()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface RateLimiter
acquirePermission, drainIfNeeded, executeCallable, executeCallable, executeCheckedSupplier, executeCheckedSupplier, executeCompletionStage, executeRunnable, executeRunnable, executeSupplier, executeSupplier, onError, onResult, onSuccess
-
Field Details
-
NAME_MUST_NOT_BE_NULL
- See Also:
-
CONFIG_MUST_NOT_BE_NULL
- See Also:
-
name
-
rateLimiterConfig
-
scheduler
-
semaphore
-
metrics
-
tags
-
eventProcessor
-
scheduledFuture
-
-
Constructor Details
-
SemaphoreBasedRateLimiter
Creates a RateLimiter.- Parameters:
name- the name of the RateLimiterrateLimiterConfig- The RateLimiter configuration.
-
SemaphoreBasedRateLimiter
public SemaphoreBasedRateLimiter(String name, RateLimiterConfig rateLimiterConfig, Map<String, String> tags) Creates a RateLimiter.- Parameters:
name- the name of the RateLimiterrateLimiterConfig- The RateLimiter configuration.tags- tags to assign to the RateLimiter
-
SemaphoreBasedRateLimiter
public SemaphoreBasedRateLimiter(String name, RateLimiterConfig rateLimiterConfig, @Nullable ScheduledExecutorService scheduler) Creates a RateLimiter.- Parameters:
name- the name of the RateLimiterrateLimiterConfig- The RateLimiter configuration.scheduler- executor that will refresh permissions
-
SemaphoreBasedRateLimiter
public SemaphoreBasedRateLimiter(String name, RateLimiterConfig rateLimiterConfig, @Nullable ScheduledExecutorService scheduler, Map<String, String> tags) Creates a RateLimiter.- Parameters:
name- the name of the RateLimiterrateLimiterConfig- The RateLimiter configuration.scheduler- executor that will refresh permissionstags- tags to assign to the RateLimiter
-
-
Method Details
-
configureScheduler
-
scheduleLimitRefresh
-
refreshLimit
void refreshLimit() -
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:
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()Reserving permissions is not supported in the semaphore based implementation. Semaphores are totally blocking by it's nature. So this non-blocking API isn't supported. UseRateLimiter.acquirePermission()- Specified by:
reservePermissionin interfaceRateLimiter- Returns:
longamount of nanoseconds you should wait for reserved permissions. if negative, it means you failed to reserve.- Throws:
UnsupportedOperationException- always for this implementation
-
reservePermission
public long reservePermission(int permits) Description copied from interface:RateLimiterReserves 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.- Throws:
UnsupportedOperationException- always for this implementation- See Also:
-
drainPermissions
public void drainPermissions()Description copied from interface:RateLimiterDrains all the permits left in the current period.- Specified by:
drainPermissionsin interfaceRateLimiter
-
getName
Get the name of this RateLimiter- Specified by:
getNamein interfaceRateLimiter- Returns:
- the name of this RateLimiter
-
getMetrics
Get the Metrics of this RateLimiter.- Specified by:
getMetricsin interfaceRateLimiter- Returns:
- the Metrics of this RateLimiter
-
getEventPublisher
Description copied from interface:RateLimiterReturns an EventPublisher which can be used to register event consumers.- Specified by:
getEventPublisherin interfaceRateLimiter- Returns:
- an EventPublisher
-
getRateLimiterConfig
Get the RateLimiterConfig of this RateLimiter.- Specified by:
getRateLimiterConfigin interfaceRateLimiter- Returns:
- the RateLimiterConfig of this RateLimiter
-
toString
-
getTags
Description copied from interface:RateLimiterReturns an unmodifiable map with tags assigned to this RateLimiter.- Specified by:
getTagsin interfaceRateLimiter- Returns:
- the tags assigned to this Retry in an unmodifiable map
-
publishRateLimiterAcquisitionEvent
private void publishRateLimiterAcquisitionEvent(boolean permissionAcquired, int permits) -
shutdown
public void shutdown()Close the scheduled task that refresh permissions if you don't use theSemaphoreBasedRateLimiteranymore. Otherwise, theSemaphoreBasedRateLimiterinstance will not be garbage collected even if you hold the reference, meaning if you create millions of instance, there could be a memory leak. (https://github.com/resilience4j/resilience4j/issues/1683)
-