Interface RecyclableRateLimiter

  • All Known Implementing Classes:
    RecyclableRateLimiterImpl, RecyclableRateLimiterWithMetrics

    public interface RecyclableRateLimiter
    A rate limiter that allows to "return" unused permits back to the pool to handle retries gracefully and allow precise control over outgoing "successful" rate, plus allows accumulating "credits" for unused permits over a time window other than 1 second.
    Author:
    vasily@wavefront.com
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default double acquire()
      Acquires a single permit, waiting until enough permits are available.
      double acquire​(int permits)
      Acquires the requested number of permits, waiting until enough permits are available.
      double getRate()
      Returns the currently configured rate per second at which new permits become available.
      boolean immediatelyAvailable​(int permits)
      Checks whether there's enough permits accumulated to cover the number of requested permits.
      void recyclePermits​(int permits)
      Return the specified number of permits back to the pool.
      void setRate​(double rate)
      Updates the rate for this RateLimiter.
      default boolean tryAcquire()
      Acquires a single permit only if can be acquired without wait.
      boolean tryAcquire​(int permits)
      Acquires the requested number of permits only if can be acquired without wait.
    • Method Detail

      • getRate

        double getRate()
        Returns the currently configured rate per second at which new permits become available.
        Returns:
        number of permits per second
      • setRate

        void setRate​(double rate)
        Updates the rate for this RateLimiter.
        Parameters:
        rate - new rate per second
      • acquire

        @CanIgnoreReturnValue
        double acquire​(int permits)
        Acquires the requested number of permits, waiting until enough permits are available.
        Parameters:
        permits - number of permits to request
        Returns:
        wait time in milliseconds
      • acquire

        @CanIgnoreReturnValue
        default double acquire()
        Acquires a single permit, waiting until enough permits are available.
      • tryAcquire

        boolean tryAcquire​(int permits)
        Acquires the requested number of permits only if can be acquired without wait.
        Parameters:
        permits - number of permits to request
        Returns:
        true if permits were acquired
      • tryAcquire

        default boolean tryAcquire()
        Acquires a single permit only if can be acquired without wait.
        Returns:
        true if permits were acquired
      • recyclePermits

        void recyclePermits​(int permits)
        Return the specified number of permits back to the pool.
        Parameters:
        permits - number of permits to return
      • immediatelyAvailable

        boolean immediatelyAvailable​(int permits)
        Checks whether there's enough permits accumulated to cover the number of requested permits.
        Parameters:
        permits - permits to check
        Returns:
        true if enough accumulated permits