This is the first day of my participation in the August Text Challenge.More challenges in August
preface
Guava is Google’s open source tool library. It provides some common and convenient operation classes, which can reduce the problems caused by null Pointers, asynchronous operations, etc. It covers many classes: Splitter, CharMatcher, Ints, Multiset Code. You know who uses it. This paper introduces the current limiting design, leaky bucket algorithm and token algorithm in Guava.
First, the term of flow limiting is certainly familiar to programmers. Why do we need to do flow limiting?
In fact, each API has a certain number of access limits, when this limit is exceeded or the number of concurrent requests exceeds the limit, the server may be overwhelmed and the system may crash. In this case, you need to consider limiting the request traffic to ensure the availability of the interface or degrade the availability. The idea is to put a fuse on the interface, and when the number of requests exceeds the expected number, the system becomes too stressed and the fuse is activated and automatically cut off.
The usual strategy is to deny unwanted access, or queue unwanted access to the service, or to divert traffic.
Second, traffic limiting algorithm
Common traffic limiting algorithms include leaky bucket algorithm and token algorithm.
Bucket algorithm
The Leaky Bucket algorithm is simple and relies on the speed at which water flows into the Leaky Bucket and exits the water at a certain speed (the port’s response rate). If the flow rate is too high, the Leaky Bucket will overflow (the access frequency exceeds the port’s response rate) and then reject the request. It can be seen that the leaky bucket algorithm will impose a limit on the data transfer rate.
disadvantages
Because the leakage rate of leaky bucket is a fixed parameter, even if the system request does not reach the limit rate or is far less than the limit rate, leaky bucket algorithm cannot make the flow rate burst to the port rate, which makes the system inefficient to some extent.
public class Limiterstest {
private static Double RATE = 11d;// Water leakage rate per second
private static Double BURST = 200d;/ / barrel size
// Update the amount of water
private Double refreshWater(Double water,Long refreshTime){
Long now = System.currentTimeMillis();
if(water == 0d) {return water;
}
BigDecimal bigResult = BigDecimal.valueOf(now - refreshTime).divide(BigDecimal.valueOf(1000));
System.out.println("Current leakage :" + bigResult.intValue() * RATE);
water = Math.max(0,water - bigResult.intValue() * RATE);
return water;
}
/** * Leaky bucket pseudo-algorithm */
@Test
public void testLeakyBucket(a){
int count = 0;
Long refreshTime = System.currentTimeMillis();// Last updated point offeset concept
Double water = 20d;// Latest water quantity (dynamic)
while (count < 500) {try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
water = refreshWater(water,refreshTime);
refreshTime = System.currentTimeMillis();
// If the water is not full, add more water
if(water < BURST){
System.out.println("Water is not full, current water level :" + water);
}else {
System.out.println("The water is full!!!! , current water level :" + water);
}
water= water + 20; count ++; }}}Copy the code