当前位置:网站首页>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个时间段内).
边栏推荐
猜你喜欢
随机推荐
开发工具篇第七讲:阿里云日志查询与分析
Smart Pointer Notes
分库分表ShardingSphere-JDBC笔记整理
【无2022上海市安全员A证考试题库及模拟考试
【电商运营】社交媒体营销策略该如何制定?
智能指针笔记
网络协议1
论文笔记:BBN: Bilateral-Branch Network with Cumulative Learningfor Long-Tailed Visual Recognition
一种基于共识机制的数字集群终端防失控方案研究
IP-Guard如何禁止运行U盘程序
Resize() usage row pit in vector
1815. Get the maximum number of groups of fresh donuts state compression
Layered Architecture & SOA Architecture
视觉任务种常用的类别文件之一json文件
交换机和路由器技术-36-端口镜像
走出迷宫的最短路径
剑指offer_抽象建模能力
如何将360全景图导出高清短视频分享到视频平台上?
Summary of c language fprintf, fscanf, sscanf and sprintf function knowledge points
延长经济保险(jeecgboot)









