Java基于时间窗口的限流算法

该代码实现了一个Java版的限流输入流,通过CurrentLimiterInputStream类限制读取数据的速度。CurrentLimiterIoUtils工具类提供了便捷的方法来复制输入流到输出流时进行限速。CurrentLimiterVo类用于配置限流参数,如最大速率和时间窗口。
摘要由CSDN通过智能技术生成

Java基于时间窗口的限流算法

算法流程

在这里插入图片描述

CurrentLimiterInputStream

package com.liangzhm.io;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Classname CurrentLimiterInputStream
 * @Description TODO
 * @Date 2023/1/18 10:22
 * @Author liangzhm
 * @Version 1.0
 */
public class CurrentLimiterInputStream extends BufferedInputStream {

    private static int DEFAULT_BUFFER_SIZE = 8192;

    private CurrentLimiterVo streamLimitVo;

    public CurrentLimiterInputStream(InputStream inputStream, CurrentLimiterVo streamLimitVo) {
        this(inputStream, DEFAULT_BUFFER_SIZE, streamLimitVo);
    }

    public CurrentLimiterInputStream(InputStream inputStream, int size) {
        this(inputStream, size, null);
    }

    public CurrentLimiterInputStream(InputStream inputStream, int size, CurrentLimiterVo streamLimitVo) {
        super(inputStream, size);
        this.streamLimitVo = streamLimitVo;
    }

    @Override
    public int read(byte b[], int off, int len) throws IOException {
        int bytes = super.read(b, off, len);
        if (streamLimitVo != null) {
            streamLimitVo.limit(bytes);
        }
        return bytes;
    }
}

CurrentLimiterIoUtils

package com.liangzhm.io;

import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @Classname CurrentLimiterIoUtils
 * @Description TODO
 * @Date 2023/1/18 13:47
 * @Author liangzhm
 * @Version 1.0
 */
public class CurrentLimiterIoUtils {

    /**
     * @param in
     * @param out
     * @param maxRate 最大速率,单位kb,如:限速200kb/s,则maxRate填写200
     * @throws IOException
     */
    public static void copy(InputStream in, OutputStream out, int maxRate) throws IOException {
        copy(in, out, CurrentLimiterVo.of(maxRate));
    }


    /**
     * @param in
     * @param out
     * @param maxRate    最大速率,单位kb,如:限速200kb/s,则maxRate填写200
     * @param timeWindow 时间窗口,单位秒,如:限速200kb/s,则timeWindow填写1
     * @throws IOException
     */
    public static void copy(InputStream in, OutputStream out, int maxRate, int timeWindow) throws IOException {
        copy(in, out, CurrentLimiterVo.of(timeWindow, maxRate));
    }

    /**
     * @param in
     * @param out
     * @param currentLimiterVo
     * @throws IOException
     */
    public static void copy(InputStream in, OutputStream out, CurrentLimiterVo currentLimiterVo) throws IOException {
        StreamUtils.copy(new CurrentLimiterInputStream(in, currentLimiterVo), out);
    }
}

CurrentLimiterVo

package com.liangzhm.io;

import java.util.concurrent.TimeUnit;

/**
 * @Classname CurrentLimiterVo
 * @Description TODO
 * @Date 2023/1/18 12:07
 * @Author liangzhm
 * @Version 1.0
 */
public class CurrentLimiterVo {

    private static final int ONE = 1;

    private static final int KB = 1024;

    private static final int MILLION = 1000_000;

    /**
     * 纳秒
     */
    private long timeWindow;

    private int chunk;

    private long previousTime;

    private int currentBytes = 0;

    /**
     * 单位KB
     */
    private int maxRate;

    private CurrentLimiterVo(int maxRate) {
        this(ONE, maxRate);
    }

    private CurrentLimiterVo(int timeWindow, int maxRate) {
        if (timeWindow < 0) {
            throw new RuntimeException("timeWindow不合法");
        }
        if (maxRate < 0) {
            throw new RuntimeException("maxRate不合法");
        }
        this.maxRate = maxRate;
        this.timeWindow = TimeUnit.SECONDS.toNanos(timeWindow);
        this.chunk = this.maxRate * KB;
    }

    /**
     * @param maxRate
     * @return
     */
    public static CurrentLimiterVo of(int maxRate) {
        return new CurrentLimiterVo(maxRate);
    }

    /**
     * @param timeWindow
     * @param maxRate
     * @return
     */
    public static CurrentLimiterVo of(int timeWindow, int maxRate) {
        return new CurrentLimiterVo(timeWindow, maxRate);
    }

    public void limit(int bytes) {
        if (bytes <= 0) {
            return;
        }
        this.currentBytes += bytes;
        if (this.previousTime == 0) {
            this.previousTime = System.nanoTime();
        }
        while (this.currentBytes >= chunk) {
            long passTime = System.nanoTime() - this.previousTime;
            long missedTime = this.timeWindow - passTime;
            if (missedTime > 0) {
                try {
                    Thread.sleep(missedTime / MILLION, (int) (missedTime % MILLION));
                } catch (InterruptedException e) {
                }
            }
            this.currentBytes -= chunk;
            this.previousTime = System.nanoTime();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值