Class SemaphoreBasedRateLimiter

    • Constructor Detail

      • SemaphoreBasedRateLimiter

        public SemaphoreBasedRateLimiter​(java.lang.String name,
                                         RateLimiterConfig rateLimiterConfig)
        Creates a RateLimiter.
        Parameters:
        name - the name of the RateLimiter
        rateLimiterConfig - The RateLimiter configuration.
      • SemaphoreBasedRateLimiter

        public SemaphoreBasedRateLimiter​(java.lang.String name,
                                         RateLimiterConfig rateLimiterConfig,
                                         io.vavr.collection.Map<java.lang.String,​java.lang.String> tags)
        Creates a RateLimiter.
        Parameters:
        name - the name of the RateLimiter
        rateLimiterConfig - The RateLimiter configuration.
        tags - tags to assign to the RateLimiter
      • SemaphoreBasedRateLimiter

        public SemaphoreBasedRateLimiter​(java.lang.String name,
                                         RateLimiterConfig rateLimiterConfig,
                                         @Nullable
                                         java.util.concurrent.ScheduledExecutorService scheduler)
        Creates a RateLimiter.
        Parameters:
        name - the name of the RateLimiter
        rateLimiterConfig - The RateLimiter configuration.
        scheduler - executor that will refresh permissions
      • SemaphoreBasedRateLimiter

        public SemaphoreBasedRateLimiter​(java.lang.String name,
                                         RateLimiterConfig rateLimiterConfig,
                                         @Nullable
                                         java.util.concurrent.ScheduledExecutorService scheduler,
                                         io.vavr.collection.Map<java.lang.String,​java.lang.String> tags)
        Creates a RateLimiter.
        Parameters:
        name - the name of the RateLimiter
        rateLimiterConfig - The RateLimiter configuration.
        scheduler - executor that will refresh permissions
        tags - tags to assign to the RateLimiter
    • 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:
        changeTimeoutDuration in interface RateLimiter
        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 interface RateLimiter
        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 is 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.

        Specified by:
        acquirePermission in interface RateLimiter
        Parameters:
        permits - number of permits - use for systems where 1 call != 1 permit
        Returns:
        true if a permit was acquired and false if 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. Use RateLimiter.acquirePermission()
        Specified by:
        reservePermission in interface RateLimiter
        Returns:
        long amount of nanoseconds you should wait for reserved permissions. if negative, it means you failed to reserve.
        Throws:
        java.lang.UnsupportedOperationException - always for this implementation
      • reservePermission

        public long reservePermission​(int permits)
        Description copied from interface: RateLimiter
        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 your RateLimiterConfig.getTimeoutDuration() is less then time to wait for permission.
        Specified by:
        reservePermission in interface RateLimiter
        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.
        Throws:
        java.lang.UnsupportedOperationException - always for this implementation
        See Also:
        reservePermission()
      • getName

        public java.lang.String getName()
        Get the name of this RateLimiter
        Specified by:
        getName in interface RateLimiter
        Returns:
        the name of this RateLimiter
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getTags

        public io.vavr.collection.Map<java.lang.String,​java.lang.String> getTags()
        Description copied from interface: RateLimiter
        Returns an unmodifiable map with tags assigned to this RateLimiter.
        Specified by:
        getTags in interface RateLimiter
        Returns:
        the tags assigned to this Retry in an unmodifiable map