请求限流
滑动窗口的实现
实现
LeapArray
Sentinel 中滑动窗口算法的核心类,先了解一下他的核心成员变量
public abstract class LeapArray<T> {
//毫秒时间周期,默认60*1000,例如计算QPS时,为1000
protected int intervalInMs;
//窗口数量,默认60
protected int sampleCount;
//窗口时间长度,毫秒数,默认1000ms 该值 = intervalInMs / sampleCount
protected int windowLengthInMs;
// 存储时间窗口的数组
protected final AtomicReferenceArray<WindowWrap<T>> array;
public LeapArray(int sampleCount, int intervalInMs) {
AssertUtil.isTrue(sampleCount > 0, "bucket count is invalid: " + sampleCount);
AssertUtil.isTrue(intervalInMs > 0, "total time interval of the sliding window should be positive");
AssertUtil.isTrue(intervalInMs % sampleCount == 0, "time span needs to be evenly divided");
this.windowLengthInMs = intervalInMs / sampleCount;
this.intervalInMs = intervalInMs;
this.sampleCount = sampleCount;
this.array = new AtomicReferenceArray<>(sampleCount);
}
}