限流--基于某个滑动时间窗口限流

基于linkedqueue 的滑动时间窗口限流

package com.heshen.algorithm.leakybucket;

import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.IntStream;

public class TimeWindow {
    private ConcurrentLinkedQueue<Long> queue = new ConcurrentLinkedQueue<Long>();

    private int seconds;

    private int max;
    public TimeWindow(int max, int timeWindowOfSeconds){
        this.seconds = timeWindowOfSeconds;
        this.max = max;

        new Thread(new Runnable(){

            @Override
            public void run() {

                for(;;){
                    clean();
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }

            }
        }).start();
    }

    /**
     * 获取令牌,并且添加时间
     */
    public void take(){


        long start = System.currentTimeMillis();
        try {


            int size = sizeOfValid();
            if (size > max - 1) {
                System.err.println(" full queue:size:" + queue.size() + ",max:" + max + ",queue:" + printQueue());
                throw new IllegalStateException("full");
            }
            synchronized (queue) {
                if (sizeOfValid() > max - 1) {
                    System.err.println(" full queue: in synchronized,size:" + queue.size() + ",max:" + max + ",queue:" + printQueue());
                    throw new IllegalStateException("full");

                }
                this.queue.offer(System.currentTimeMillis());
            }

            System.out.println(" queue: d,size:" + queue.size() + ",max:" + max + ",queue:" + printQueue());
        }finally {
            System.out.println("cost:"+(System.currentTimeMillis()  - start) + " ms");
        }

    }

    private String printQueue(){

        Iterator<Long> it = queue.iterator();
        StringBuilder sb = new StringBuilder();
        while (it.hasNext()){
           Long t = it.next();
           sb.append(" ").append(t);
        }
        return sb.toString();
    }



    public int sizeOfValid(){


        Iterator<Long> it = queue.iterator();

        Long ms = System.currentTimeMillis() - seconds * 1000;

        int count = 0;
        while (it.hasNext()){
           long t = it.next();
           if(t > ms){
               count++;
           }
        }

        return count;
    }

    /**
     * 清理
     */
    public void clean(){
        Long c = System.currentTimeMillis() - seconds * 1000;

        Long tl = null;

        System.out.println("peek: "+queue.peek() +"c:"+ c);

        while((tl = queue.peek()) != null && tl < c){
            System.out.println("peek: t:"+ tl);
            queue.poll();
        }
    }
    public static void main(String[] args){

        final TimeWindow timeWindow = new TimeWindow(200, 2);

        IntStream.range(0, 100).forEach((i)->{
            new Thread(() -> {

                for(;;){
                    System.out.println("before take i:"+i);

                    try {
                        Thread.sleep(new Random().nextInt(20)* 100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try{
                        timeWindow.take();

                        System.out.println("some option, i:"+i);

                    }catch (Exception e){
                        System.err.println(" take i:"+i +", encounter error:"+ e.getMessage());
                        try {
                            Thread.sleep(10L);
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }
                        continue;

                    }

                    System.out.println("after take i:"+i);
                }


            }).start();
        });

    }
}

需求在某个滑动的时间窗口内,限定访问次数

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值