• 时间滑动窗口限制请求次数


    一、时间窗口固定

    使用map存储,key为唯一值,value为一个实体,存着开始时间和次数,取出来判断是否为空,如果为空就新建一个对象进去并且如果不为空并且当前时间-取出来的时间>最大时间限制值,重新赋值value,value=当前时间,次数1,返回可以请求,如果上述条件不满足,则判断次数是否>=最大限制,如果大于就返回不让请求,如果小于,则次数加1返回可以请求。

    缺点:时间快结束和时间开始的时候会在xxx区间的时候大于某段时间内最请求 所以看下面时间滑动窗口解决

    import java.util.HashMap;
    import java.util.Map;
    
    public class RequestLimiter {
        private static final int MAX_REQUESTS = 5; // 最大请求次数
        private static final long MAX_TIME_WINDOW = 60 * 1000; // 最大时间窗口,单位:毫秒
    
        private Map<String, RequestEntity> requestMap = new HashMap<>();
    
        public boolean canMakeRequest(String key) {
            long currentTime = System.currentTimeMillis();
            RequestEntity entity = requestMap.get(key);
    
            if (entity == null) {
                // 如果唯一标识符不存在于Map中,创建一个新的实体对象
                entity = new RequestEntity(currentTime, 1);
                requestMap.put(key, entity);
                return true;
            } else {
                // 如果唯一标识符存在
                long elapsedTime = currentTime - entity.getStartTime();
    
                if (elapsedTime > MAX_TIME_WINDOW) {
                    // 如果时间窗口过期,重新设置实体对象的值
                    entity.setStartTime(currentTime);
                    entity.setCount(1);
                    return true;
                } else if (entity.getCount() < MAX_REQUESTS) {
                    // 如果在时间窗口内,但请求次数未达到限制,增加次数
                    entity.setCount(entity.getCount() + 1);
                    return true;
                } else {
                    // 如果次数已达到限制,不允许请求
                    return false;
                }
            }
        }
    
        public static void main(String[] args) {
            RequestLimiter requestLimiter = new RequestLimiter();
    
            String key = "user123";
    
            for (int i = 0; i < 10; i++) {
                if (requestLimiter.canMakeRequest(key)) {
                    System.out.println("Request allowed");
                } else {
                    System.out.println("Request not allowed");
                }
            }
        }
    }
    
    class RequestEntity {
        private long startTime;
        private int count;
    
        public RequestEntity(long startTime, int count) {
            this.startTime = startTime;
            this.count = count;
        }
    
        public long getStartTime() {
            return startTime;
        }
    
        public void setStartTime(long startTime) {
            this.startTime = startTime;
        }
    
        public int getCount() {
            return count;
        }
    
        public void setCount(int count) {
            this.count = count;
        }
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    二、时间滑动窗口

    public class TimeSlidingWindow {
        private Deque<Long> window;  // 存储时间戳的双端队列
        private int windowSize;      // 窗口大小
        private long windowDuration; // 窗口持续时间(毫秒)
    
        public TimeSlidingWindow(int windowSize, long windowDuration) {
            this.window = new ArrayDeque<>();
            this.windowSize = windowSize;
            this.windowDuration = windowDuration;
        }
    
        public Boolean addTimestamp(long timestamp) {
            // 移除超出窗口持续时间的时间戳
            long threshold = timestamp - windowDuration;
            //如果不为空&&拿出第一比较  超出窗口持续时间的时间戳
            while (!window.isEmpty() && window.peekFirst() < threshold) {
            	//移除
                window.pollFirst();
            }
    
    		if(window.size() >= windowSize) {
    		 return false;
    		}
            // 添加新的时间戳到窗口末尾
            window.offerLast(timestamp);
            return true;
        }
       }
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  • 相关阅读:
    从 Solana 课程顺利毕业获得高潜岗位,他的 Web3 开发探险之旅
    如何用好Nginx的gzip指令
    数据库编程sqlite3库安装及使用
    APP广告竞价机制:头部竞价与瀑布流
    计算机网络基础之计算机网络组成与分类
    SpringBoot、Vue、Nginx配置 https 并部署发布
    jmeter下载地址
    android--TextView在刷新时宽度变大的问题排查记录
    利用多计数器级联的方法,使用通道1与通道2,利用1MHz的输入脉冲信号,产生1s高电平,1s低电平的方波,并用来控制IED小灯的亮灭的原理图及其代码
    使用Git实现windows与linux服务器的项目代码同步
  • 原文地址:https://blog.csdn.net/weixin_49390750/article/details/134171895