当前位置:网站首页>guava RateLimiter uniform current limit
guava RateLimiter uniform current limit
2022-08-11 05:03:00 【pilaf1990】
guava的pom依赖:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
guava采用了令牌桶算法,That is, put tokens into the bucket at a constant rate,需要通过RateLimiter的acquireMethod blocking after getting the token,to continue the task(Such as request processing and other code),So as to achieve the purpose of uniform current limiting.
guava的RateLimiterCurrent limiter creation method:
RateLimiter.create(5);
其中createThe parameters of the method indicate yes1The number of tokens allowed to be acquired in seconds,即QPS.
createIt is created by default inside the methodSmoothBursty类型的RateLimiter,It will be relatively evenQPS分散到1time periods in seconds.
比如这儿的create(5),表示1Only available in seconds5个令牌,并且前0.2Only available in seconds1个,0.2-0.4It can only be obtained in seconds1个,0.4-0.6秒,0.6-0.8秒,0.8-1.0Seconds can also be obtained separately1个令牌.The benefit of this is to prevent uneven requests,如果在1At the beginning of the second, the tokens are all used up,Then the token is also obtained at the end of the previous second,The number of tokens acquired in the second at the end of the previous second and the beginning of this second is exceededQPSThe defined number of tokens allowed to be obtained.
下面看一段代码:
public class RateLimiterDemo {
public static void main(String[] args) throws Exception{
RateLimiter rateLimiter = RateLimiter.create(5);
for (int i = 0; i < 10; i++) {
System.out.println(rateLimiter.tryAcquire());
}
}
}
执行后输出
true
false
false
false
false
false
false
false
false
false
只有第一次tryAcquire返回true,Returned several times laterfalse,That is, only the first attempt to obtain the token succeeds,后边的9have not been obtained,因为for循环执行很快,Basically all ahead0.2seconds to try to get the token,It can only be obtained1个(5tokens are scattered1秒内,前0.2Only one token can be obtained per second).
将forAdd sleep to the loop:
public class RateLimiterDemo {
public static void main(String[] args) throws Exception{
RateLimiter rateLimiter = RateLimiter.create(5);
for (int i = 0; i < 10; i++) {
Thread.sleep(200);
System.out.println(rateLimiter.tryAcquire());
}
}
}
输出的结果是10个true:
true
true
true
true
true
true
true
true
true
true
That is to say, the token is obtained every time.Because it sleeps after each fetch200毫秒,1秒除以200毫秒等于5,So each time period can get the token(我们通过create(5)The number of tokens that can be obtained in one second is limited5,会分散到5个时间段内).
边栏推荐
猜你喜欢
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/data/xxxx
Do you understand how the Selenium automated testing framework works?
每周推荐短视频:你常用的拍立淘,它的前身原来是这样的!
Zabbix builds enterprise-level monitoring and alarm platform
findViewById返回null的问题
async(异步)和await的使用
Overview of the JVM garbage collection and mechanism
[E-commerce operation] How to formulate a social media marketing strategy?
网络协议1
CAD2020 打开错误报告 e06d7363h Exception at 13644F69h
随机推荐
Switch and Router Technology - 36-Port Mirroring
[QNX Hypervisor 2.2 User Manual] 10.16 vdev virtio-blk
我的LaTeX入门
Switch and Router Technology - 28 - NSSA Areas for OSPF
map and set - natural search and lookup semantics
视觉任务种常用的类别文件之一json文件
Unity WebGL RuntimeError: integer overflow(整数溢出问题)
Development Tools Lecture 7: Alibaba Cloud Log Query and Analysis
2022年质量员-土建方向-通用基础(质量员)考试模拟100题及在线模拟考试
一种基于共识机制的数字集群终端防失控方案研究
[FPGA tutorial case 50] Control case 2 - FPGA-based PD controller verilog implementation
金仓数据库 KingbaseGIS 使用手册(6.10. 几何对象操作运算符)
I wrote some code in my resume, but I still can't pass the interview
交换机和路由器技术-31-扩展ACL
Switch and Router Technology-29-OSPF Virtual Link
在 关闭页面/卸载(unload)文档 之前向服务器发送请求
分库分表ShardingSphere-JDBC笔记整理
Merkel Studio--OpenEuler Training Notes (1)
Do you understand how the Selenium automated testing framework works?
[QNX Hypervisor 2.2用户手册]10.16 vdev virtio-blk