add SlideWindow
This commit is contained in:
@@ -27,6 +27,7 @@ package io.github.ehlxr.rate;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 滑动时间窗口限流工具
|
* 滑动时间窗口限流工具
|
||||||
@@ -65,7 +66,7 @@ public class SlideWindow {
|
|||||||
*
|
*
|
||||||
* @return 是否允许通过
|
* @return 是否允许通过
|
||||||
*/
|
*/
|
||||||
public synchronized boolean isGo() {
|
public synchronized boolean acquire() {
|
||||||
// 获取当前时间
|
// 获取当前时间
|
||||||
long nowTime = System.currentTimeMillis();
|
long nowTime = System.currentTimeMillis();
|
||||||
// 如果队列还没满,则允许通过,并添加当前时间戳到队列开始位置
|
// 如果队列还没满,则允许通过,并添加当前时间戳到队列开始位置
|
||||||
@@ -90,14 +91,36 @@ public class SlideWindow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized void tryAcquire() throws InterruptedException {
|
||||||
|
long nowTime = System.currentTimeMillis();
|
||||||
|
if (list.size() < count) {
|
||||||
|
list.add(0, nowTime);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
long l = nowTime - list.get(count - 1);
|
||||||
|
if (l <= timeWindow) {
|
||||||
|
// 等待
|
||||||
|
TimeUnit.MILLISECONDS.sleep(timeWindow - l);
|
||||||
|
tryAcquire();
|
||||||
|
} else {
|
||||||
|
list.remove(count - 1);
|
||||||
|
list.add(0, nowTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
SlideWindow slideWindow = new SlideWindow(5, 1000);
|
SlideWindow slideWindow = new SlideWindow(5, 1000);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// 任意1秒内,只允许5次通过
|
// 任意1秒内,只允许5次通过
|
||||||
if (slideWindow.isGo()) {
|
// if (slideWindow.acquire()) {
|
||||||
System.out.println(System.currentTimeMillis());
|
// System.out.println(System.currentTimeMillis());
|
||||||
}
|
// }
|
||||||
|
slideWindow.tryAcquire();
|
||||||
|
|
||||||
|
System.out.println(System.currentTimeMillis());
|
||||||
|
|
||||||
// 睡眠0-10ms
|
// 睡眠0-10ms
|
||||||
Thread.sleep(new Random().nextInt(10));
|
Thread.sleep(new Random().nextInt(10));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user