This article mainly studies the relevant content of Java programming guava RateLimiter, as follows.
Token bucket algorithm
Scenario 1 Application in traffic supervision
Consensated access rate (CAR) is one of the commonly used technologies for traffic supervision. It can be applied in port inlet and exit directions, and is generally applied in the inlet direction. Its supervision principle is shown in Figure 1.
a. Put tokens to the token bucket at a specific rate
b. Classify the packets first according to the preset matching rules. Messages that do not meet the matching rules do not need to be processed by the token bucket and are sent directly;
c. For packets that meet the matching rules, the token bucket needs to be processed. When there are enough tokens in the bucket, the message can be continued to be sent, and the number of tokens in the token bucket is reduced accordingly according to the length of the message;
d. When there is insufficient token in the token bucket, the message will not be sent. The message can only be sent until a new token is generated in the bucket. This can limit the traffic of the packet to be less than or equal to the speed of token generation, achieving the purpose of limiting the traffic.
The second scenario: used for flow control, overload protection in the application field.
Examples of use:
public class RateLimiterTest { public static void main(String[] args) { final RateLimiter rateLimiter = RateLimiter.create(2.0); for (int i = 0; i < 100; i++) { rateLimiter.acquire(); // Print twice per second System.out.println(i); } } }Summarize
The above is all the content of this article about the analysis of the Java programming guava RateLimiter instance, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!