当前位置:   article > 正文

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

java 时间窗口

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;
    }
}

  • 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

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);
    }
}

  • 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

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();
        }
    }
}

  • 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
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/274811
推荐阅读
相关标签
  

闽ICP备14008679号