滑动窗口只有两个的话,无法避免固定窗口存在的问题,依然存在1秒中之内请求超出阈值的情况?
- Dominant language
- Java
- Stars
- 23.1k
- Forks
- 8.1k
- PR merge metrics
- No merged PRs in 30d
Description
无法避免固定窗口存在存在的问题,依然存在1秒中之内请求超出阈值的情况,特别是这里只有两个小窗口,极端情况请求会翻倍

改造了Demo项目中的代码示例:
```java
// 限流规则(已启动的时候调用)
private static void initFlowQpsRule() {
List rules = new ArrayList();
FlowRule rule1 = new FlowRule();
rule1.setResource("test");
// set limit qps to 5
rule1.setCount(5);
rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule1.setLimitApp("default");
rules.add(rule1);
FlowRuleManager.loadRules(rules);
}
// 超出限流规则限制代码
@GetMapping("/foo")
public String apiFoo(@RequestParam(required = false) Long t) throws Exception {
if (t == null) {
t = System.currentTimeMillis();
}
long start = 0;
while (System.currentTimeMillis() % 500 != 0) {
start = System.currentTimeMillis();
continue;
}
System.out.println("开始时间:" + start);
Thread.sleep(300);
long taskstart = System.currentTimeMillis();
System.out.println("执行任务开始时间:" + taskstart);
for (int i = 0; i < 5; i++) {
service.test();
}
while (System.currentTimeMillis() - start < 500) {
continue;
}
System.out.println(System.currentTimeMillis());
Thread.sleep(500);
for (int i = 0; i < 5; i++) {
service.test();
}
long taskend = System.currentTimeMillis();
System.out.println("执行任务结束时间:" + taskend);
System.out.println("总执行时间:" + (taskend - start));
System.out.println("实际执行时间:" + (taskend - taskstart));
return null;
}
```
结果:

从结果来看,配置的规则是5,但实际上,还是存在1秒中之内资源调用了10次。是我使用的方式不对,还是说这种请求情况就是允许的
Contributor guide
Research direction
Start with the Demo project's initFlowQpsRule and /foo endpoint, then reproduce the reported sequence across the two 500 ms windows with a QPS limit of 5. Compare the observed ten calls within one second with Sentinel's documented sliding-window behavior and determine whether this is expected or indicates a defect.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100