Class BlueThrottle


  • public class BlueThrottle
    extends Object
    Implements a token-bucket based rate limiting mechanism with optional probabilistic dropping inspired by the BLUE queue management algorithm [1]. The throttle provides the checkLimit(int) method which provides a binary yes/no decision. The core token bucket algorithm starts with an initial set of tokens based on the maxTokens setting. Tokens are dispensed each checkLimit(int) call, which fails if there are not enough tokens to satisfy a given request. The token bucket refills over time, providing fillCount tokens every fillTime milliseconds, capping at maxTokens. This design allows the throttle to allow short bursts to pass, while still capping the total number of requests per time interval. One issue with a pure token bucket approach for something like request or connection throttling is that the wall clock arrival time of requests affects the probability of a request being allowed to pass or not. Under constant load this can lead to request starvation for requests that constantly arrive later than the majority. In an attempt to combat this, this throttle can also provide probabilistic dropping. This is enabled anytime freezeTime is set to a value other than -1. The probabilistic algorithm starts with an initial drop probability of 0, and adjusts this probability roughly every freezeTime milliseconds. The first request after freezeTime, the algorithm checks the token bucket. If the token bucket is empty, the drop probability is increased by dropIncrease up to a maximum of 1. Otherwise, if the bucket has a token deficit less than decreasePoint * maxTokens, the probability is decreased by dropDecrease. Given a call to checkLimit(int), requests are first dropped randomly based on the current drop probability, and only surviving requests are then checked against the token bucket. When under constant load, the probabilistic algorithm will adapt to a drop frequency that should keep requests within the token limit. When load drops, the drop probability will decrease, eventually returning to zero if possible. [1] "BLUE: A New Class of Active Queue Management Algorithms"
    • Constructor Detail

      • BlueThrottle

        public BlueThrottle()
    • Method Detail

      • setConnectionWeightEnabled

        protected static void setConnectionWeightEnabled​(boolean enabled)
      • setMaxTokens

        public void setMaxTokens​(int max)
      • setFillTime

        public void setFillTime​(int time)
      • setFillCount

        public void setFillCount​(int count)
      • setFreezeTime

        public void setFreezeTime​(int time)
      • setDropIncrease

        public void setDropIncrease​(double increase)
      • setDropDecrease

        public void setDropDecrease​(double decrease)
      • setDecreasePoint

        public void setDecreasePoint​(double ratio)
      • getMaxTokens

        public int getMaxTokens()
      • getFillTime

        public int getFillTime()
      • getFillCount

        public int getFillCount()
      • getFreezeTime

        public int getFreezeTime()
      • getDropIncrease

        public double getDropIncrease()
      • getDropDecrease

        public double getDropDecrease()
      • getDecreasePoint

        public double getDecreasePoint()
      • getDropChance

        public double getDropChance()
      • getDeficit

        public int getDeficit()
      • getRequiredTokensForGlobal

        public int getRequiredTokensForGlobal()
      • getRequiredTokensForLocal

        public int getRequiredTokensForLocal()
      • getRequiredTokensForRenew

        public int getRequiredTokensForRenew()
      • isConnectionWeightEnabled

        public boolean isConnectionWeightEnabled()
      • checkLimit

        public boolean checkLimit​(int need)
      • checkBlue

        public boolean checkBlue​(long now)