alibaba / alibaba/Sentinel

Is there any mistake in reducing the operation of the token in WarmUpController?

Open
#953 1 comment 0 reactions 0 assignees View on GitHub
area/flow-control kind/question
Dominant language
Java
Stars
23.1k
Forks
8.1k
PR merge metrics
No merged PRs in 30d

Description

```java
protected void syncToken(long passQps) {
long currentTime = TimeUtil.currentTimeMillis();
currentTime = currentTime - currentTime % 1000;
long oldLastFillTime = lastFilledTime.get();
if (currentTime <= oldLastFillTime) {
return;
}

long oldValue = storedTokens.get();
long newValue = coolDownTokens(currentTime, passQps);

if (storedTokens.compareAndSet(oldValue, newValue)) {
long currentValue = storedTokens.addAndGet(0 - passQps);
if (currentValue < 0) {
storedTokens.set(0L);
}
lastFilledTime.set(currentTime);
}

}
```
在 coolDownTokens 中增加 token 时,有时间维度的计算:

```java
newValue = (long)(oldValue + (currentTime - lastFilledTime.get()) * count / 1000);
```
简单理解就是以每秒 count 的数量进行增加。

但是在 coolDownTokens 出来以后,在减少 token 的操作中,并没有将时间考虑进去:

```java
long currentValue = storedTokens.addAndGet(0 - passQps);
```
当然,这一段代码本身很复杂,也许是我理解有误。

Contributor guide

Open the contributing guide

Research direction

Start in WarmUpController.syncToken and trace coolDownTokens, focusing on the elapsed-time calculation with TimeUtil.currentTimeMillis(), lastFilledTime, storedTokens, and passQps. Compare the refill and subtraction semantics and check existing coverage for elapsed-time behavior; done means the suspected token-accounting mismatch is verified and resolved or documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.