限流及其算法

限流就是在高访问量的情况下对于可能拖死系统的访问做拒绝处理或者部分拒绝处理

  • 滑动窗口
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;

public class TimeWindow {
    //通过计数器 会有双倍"限制"流量的问题  在timeout 时间内 可能最后一点时间 涌入limit 次调用 在下一个timeout开始时间 涌入limit 流量 显然不符合预期
    private ConcurrentLinkedQueue<Long> queue = new ConcurrentLinkedQueue<Long>();
    /**
     * 在seconds 内的最大调用次数为max
     */
    private int seconds;
    private int max;


    public TimeWindow(int seconds,int max){
        this.seconds = seconds;
        this.max = max;
        Thread t = new Thread(()->{
            try{
                Thread.sleep((seconds - 1) * 1000L);
            }catch(Exception e){
                e.printStackTrace();
            }
            clean();

        });
    }
    public void clean(){
        long now = System.currentTimeMillis();
        while(!queue.isEmpty()){
            if(now - queue.peek() > seconds * 1000L ){
                System.out.println("清理数据"+queue.poll());
            }
        }
    }

    /**
     * 获取令牌 添加时间
     * @return
     */
    public boolean take(){
        int size = sizeOfValid();
        if(size < max){
            return false;
        }
        synchronized(queue){
            if(sizeOfValid() > max){
                return false;
            }
            long now = System.currentTimeMillis();
            queue.offer(now);
            return true;
        }
    }
    public int sizeOfValid(){
        Iterator<Long> it = queue.iterator();
        long now = System.currentTimeMillis();
        int count = 0;
        while(it.hasNext()){
            long tem = it.next();
            if(now - tem < seconds *1000L){
                count++;
            }
        }
        return count;
    }
}

令牌桶 Token Bucket

public class TokenBucket {
    /**
     * 在time时间的令牌的个数
     */
    private long time;
    private Double nowSize;
    /**
     * 生成令牌的速率
     */
    private double rate;
    /**
     * 桶的总数量
     */
    private Double total;

    public boolean limit() {
        long now = System.currentTimeMillis();
        nowSize = nowSize + (now - time) * rate;
        if (nowSize > total) {
            nowSize = total;
        }
        time = now;
        if (nowSize >= 1) {
            nowSize--;
            return true;
        } else {
            return false;
        }
    }
}

Leaky Bucket

public class LeakyBucket {
    private long time;
    private double nowSize;
    private double rate;
    private double total;
    public boolean limit(){
        long now = System.currentTimeMillis();
        nowSize = nowSize - (now - time) * rate;
        if(nowSize < 0){
            nowSize = 0;
        }
        time = now;
        if(nowSize + 1 < total){
            nowSize++;
            return true;
        }else{
            return false;
        }
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值