Class BasicRateLimiter


  • public class BasicRateLimiter
    extends Object
    The simplified version of Google Guava smooth rate limiter.

    The primary feature of a rate limiter is its "stable rate", the maximum rate that is should allow at normal conditions. This is enforced by "throttling" incoming requests as needed, i.e. compute, for an incoming request, the appropriate throttle time, and make the calling thread wait as much.

    The simplest way to maintain a rate of QPS is to keep the timestamp of the last granted request, and ensure that (1/QPS) seconds have elapsed since then. For example, for a rate of QPS=5 (5 tokens per second), if we ensure that a request isn't granted earlier than 200ms after the last one, then we achieve the intended rate. If a request comes and the last request was granted only 100ms ago, then we wait for another 100ms. At this rate, serving 15 fresh permits (i.e. for an acquire(15) request) naturally takes 3 seconds.

    It is important to realize that such a limiter has a very superficial memory of the past: it only remembers the last request. if the limiter was unused for a long period of time, then a request arrived and was immediately granted? This limiter would immediately forget about that past underutilization.
    • Constructor Detail

      • BasicRateLimiter

        public BasicRateLimiter​(double permitsPerSecond)
        Parameters:
        permitsPerSecond - Estimated number of permits per second.
    • Method Detail

      • setRate

        public void setRate​(double permitsPerSecond)
        Updates the stable rate.
        Parameters:
        permitsPerSecond - The new stable rate of this RateLimiter, set 0 for unlimited rate.
        Throws:
        IllegalArgumentException - If permitsPerSecond is negative or zero.
      • getRate

        public double getRate()
        Returns:
        The stable rate as permits per seconds (0 means that the rate is unlimited).
      • isUnlimited

        public boolean isUnlimited()
        Returns:
        True if the rate is not limited.