当前位置:网站首页>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个时间段内).
边栏推荐
- 一起Talk编程语言吧
- 如何阅读论文
- 交换机和路由器技术-22/23-OSPF动态路由协议/链路状态同步过程
- Switch and Router Technology - 32 - Named ACL
- Australia cyberspace security system construction
- 论文笔记:Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
- Apache初体验
- Switch and Router Technology - 36-Port Mirroring
- 关于CC 攻击
- The sword refers to offer_abstract modeling capabilities
猜你喜欢
Switch and Router Technology - 22/23 - OSPF Dynamic Routing Protocol/Link State Synchronization Process
Switch and Router Technology-31-Extended ACL
Do you understand how the Selenium automated testing framework works?
论文笔记:BBN: Bilateral-Branch Network with Cumulative Learningfor Long-Tailed Visual Recognition
每周推荐短视频:你常用的拍立淘,它的前身原来是这样的!
如何将360全景图导出高清短视频分享到视频平台上?
IP-Guard如何禁止运行U盘程序
交换机和路由器技术-29-OSPF虚链路
Switch and Router Technology - 36-Port Mirroring
Merkel Studio--OpenEuler Training Notes (1)
随机推荐
vector中resize() 用法排坑
我的LaTeX入门
视觉任务种常用的类别文件之一json文件
Switch and Router Technology-31-Extended ACL
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/data/xxxx
To break the bottleneck of transactional work, the gentleman signs the electronic contract to release the "source power" of HR!
leetcode 9. Palindromic Numbers
IP-Guard如何禁止运行U盘程序
svg-icon的使用方法(svg-sprite-loader插件)
Switch and Router Technology - 32 - Named ACL
BGP综合实验
【无2022上海市安全员A证考试题库及模拟考试
[Actual combat scene] Mall-discount event design plan
Switch and Router Technology-33-Static NAT
Do you understand how the Selenium automated testing framework works?
用白嫖的Adobe正版软件,减少应届毕业生的慢就业、不就业等现象
Paper Notes: Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
交换机和路由器技术-30-标准ACL
About CC Attacks
Apache初体验