当前位置:网站首页>guava RateLimiter均匀限流
guava RateLimiter均匀限流
2022-08-11 04:59:00 【pilaf1990】
guava的pom依赖:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
guava采用了令牌桶算法,即以恒定的速率向桶里放令牌,需要通过RateLimiter的acquire方法阻塞式地拿到令牌之后,才可以继续执行任务(比如请求处理等代码),从而达到均匀限流的目的。
guava的RateLimiter限流器创建方法:
RateLimiter.create(5);
其中create方法的参数表示的是1秒钟内允许获取的令牌数量,即QPS。
create方法内部默认会创建SmoothBursty类型的RateLimiter,它是比较均匀的将QPS分散到1秒内的各个时间段。
比如这儿的create(5),表示1秒钟内只能获取5个令牌,并且前0.2秒内只能获取1个,0.2-0.4秒内也只能获取1个,0.4-0.6秒,0.6-0.8秒,0.8-1.0秒也分别能获取1个令牌。这样做的好处是防止请求不均匀,如果在1秒的开始就把令牌都用完了,那么在上一秒结束的时候也获得了令牌,则上一秒结束和这一秒的开始所在的秒内获取的令牌数就超过了QPS的定义的允许获得的令牌数了。
下面看一段代码:
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,后边几次都返回false,即只有第一次尝试获取令牌的时候获取成功了,后边的9次都没有获取到,因为for循环执行很快,基本上都在前0.2秒内去尝试获取令牌,也就只能获取到1个(5个令牌分散在1秒内,前0.2秒只能获取一个令牌)。
将for循环中加入睡眠:
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
也就是说每次都获取到了令牌。因为每次获取后都睡眠了200毫秒,1秒除以200毫秒等于5,所以每个时间段都能获取到令牌(我们通过create(5)限制了一秒内能获取到的令牌数就是5,会分散到5个时间段内)。
边栏推荐
- I wrote some code in my resume, but I still can't pass the interview
- 4 模块三:文献阅读与研究方法
- Optimization is a kind of habit low starting point is the "standing near the critical"
- 在 关闭页面/卸载(unload)文档 之前向服务器发送请求
- Paper Notes: BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition
- MFC 进程间通信(共享内存)
- 3 Module 2: Use of scientific research tools
- 4 Module 3: Literature Reading and Research Methods
- Overview of the JVM garbage collection and mechanism
- 交换机和路由器技术-31-扩展ACL
猜你喜欢
![ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/data/xxxx](/img/02/3896b29a955ae84a0f0326f0d2cabf.png)
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/data/xxxx

Switch and Router Technology - 28 - NSSA Areas for OSPF

3 Module 2: Use of scientific research tools

Network Skill Tree

2021年网络规划设计师下午案例题

form form submission database Chinese becomes a question mark

Zabbix builds enterprise-level monitoring and alarm platform

Dry goods: The principle and practice of server network card group technology

Switch and Router Technology - 22/23 - OSPF Dynamic Routing Protocol/Link State Synchronization Process

CAN/以太网转换器 CAN与以太网互联互通
随机推荐
Switches and routers technology - 26 - configure OSPF peripheral area
交换机和路由器技术-27-OSPF路由重分发
Unity WebGL RuntimeError: integer overflow(整数溢出问题)
3 模块二:科研工具使用
How to use svg-icon (svg-sprite-loader plugin)
zabbix构建企业级监控告警平台
一起Talk编程语言吧
[FPGA tutorial case 50] Control case 2 - FPGA-based PD controller verilog implementation
交换机和路由器技术-21-RIP路由协议
交换机和路由器技术-30-标准ACL
交换机和路由器技术-35-NAT转PAT
Redis: Solve the problem of modifying the same key with distributed high concurrency
Do you understand how the Selenium automated testing framework works?
Switches and routers technology - 24 - configure OSPF single area
我的LaTeX入门
FPGA engineer interview questions collection 111~120
Kong实现禁止国外IP访问
CAN/以太网转换器 CAN与以太网互联互通
Zabbix builds enterprise-level monitoring and alarm platform
vector中resize() 用法排坑