当前位置: 首页 > news >正文

简易限流实现

需求描述

写一个1秒两个的限流工具类,2r/s

使用semaphore

代码实现-类似令牌桶算法


public class LimitHelper {private int maxLimit;private Semaphore semaphore;private int timeoutSeconds;public LimitHelper(int maxLimit, int timeoutSeconds) {this.maxLimit = maxLimit;semaphore = new Semaphore(maxLimit);this.timeoutSeconds = timeoutSeconds;this.autoRelease();}/*** 每秒钟释放两个信号出来*/private void autoRelease() {new Thread(() -> {try {while (true) {TimeUnit.SECONDS.sleep(timeoutSeconds);if (!semaphore.tryAcquire(1)) {// 无信号了semaphore.release(maxLimit);}else {// 池中有信号,并且消耗了一个,释放一个补偿semaphore.release(1);}}} catch (InterruptedException e) {e.printStackTrace();}}).start();}public boolean acquire() {try {return semaphore.tryAcquire(1, timeoutSeconds, TimeUnit.SECONDS);} catch (InterruptedException e) {throw new RuntimeException(e);}}public static void main(String[] args) {LimitHelper limitHelper = new LimitHelper(2, 1);for (int i = 0; i < 5; i++) {new Thread(() -> {while (true) {if (limitHelper.acquire()) {System.out.println( System.currentTimeMillis() / 1000+ "  " + Thread.currentThread().getName() + " 获取到令牌");try {// 业务处理TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {throw new RuntimeException(e);}} else {System.out.println(System.currentTimeMillis() / 1000 + "  "+Thread.currentThread().getName() + " 被限流了");}}}).start();}}}

结果演示

滑动窗口算法实现

如上图,红色是被限流的请求事件蓝色是允许的请求事件。

代码实现


public class LimitHelper2 {/*** 次数*/private int maxRate;/*** 多久时间内*/private int rangeTimeSeconds;/*** 历史记录*/private volatile LinkedList<Long> timeRecord;public LimitHelper2(int maxRate, int rangeTimeSeconds) {this.maxRate = maxRate;this.rangeTimeSeconds = rangeTimeSeconds;timeRecord = new LinkedList<>();}/*** 2r/s 限流实现*/private synchronized boolean acquire() {long now = System.currentTimeMillis();long min = now - (rangeTimeSeconds * 1000L);if (timeRecord.isEmpty()) {timeRecord.addLast(now);return true;}// 不为空并且size >= maxRateint count = 0;if (timeRecord.size() >= maxRate) {LinkedList<Long> newRecords = new LinkedList<>();for (int i = 0; i < maxRate; i++) {Long beforeHappenTime = timeRecord.pollLast();if (beforeHappenTime == null) {timeRecord.addLast(now);System.out.println("before时间为null");return true;}newRecords.addFirst(beforeHappenTime);if (beforeHappenTime.compareTo(min) >= 0) {count++;} else {break;}}timeRecord = newRecords;if (count >= maxRate) {return false;} else {timeRecord.addLast(now);return true;}} else {timeRecord.add(now);return true;}}public static void main(String[] args) {// 1秒2次 限流LimitHelper2 limiter = new LimitHelper2(4, 1);for (int i = 0; i < 5; i++) {new Thread(() -> {while (true) {if (limiter.acquire()) {try {System.out.println(System.currentTimeMillis() + "  " + Thread.currentThread().getName() + " 执行任务");Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}} else {System.out.println(System.currentTimeMillis() + "  " + Thread.currentThread().getName() + " 被限流");}try {Thread.sleep(500);} catch (InterruptedException e) {throw new RuntimeException(e);}}}).start();}}}

结果演示

4r/s

http://www.lryc.cn/news/399113.html

相关文章:

  • 用Qwt进行图表和数据可视化开发
  • sqlalchemy使用with_entities返回指定数据列
  • express
  • HTML网页大设计-家乡普宁德安里
  • 深度学习:从数据采集到模型测试的全面指南
  • Excel第29享:基于sum嵌套sumifs的多条件求和
  • Elasticsearch:Node.js ECS 日志记录 - Morgan
  • ChatGPT对话:Python程序自动模拟操作网页,无法弹出下拉列表框
  • Unity 之 抖音小游戏集成排行榜功能详解
  • 【学习笔记】Redis学习笔记——第13章 客户端
  • Android中的JSON解析:从基础到实践
  • 力扣-回溯法
  • 240713_昇思学习打卡-Day25-LSTM+CRF序列标注(4)
  • python requests关闭https校验
  • PG大会周五于杭州举办;Pika发布4.0;阿里云MySQL上线Zero-ETL集成能力
  • 虚拟机vmware网络设置
  • 数学建模国赛入门指南
  • Java基础之集合
  • 深度学习和NLP中的注意力和记忆
  • 自用的C++20协程学习资料
  • 【C++】优先级队列(底层代码解释)
  • 华为模拟器防火墙配置实验(二)
  • group 与查询字段
  • PlantUML 教程:绘制时序图
  • 自定义ViewGroup-流式布局FlowLayout(重点:测量和布局)
  • C++的入门基础(二)
  • 显示产业如何突破芯片短板
  • STM32HAL库+ESP8266+cJSON+微信小程序_连接华为云物联网平台
  • debian或Ubuntu中开启ssh允许root远程ssh登录的方法
  • C++《日期》实现