[ISSUES #11626] 优化Nacos的默认反脆弱插件 (#11625)

* Update LocalSimpleCountRateCounter.java

fixbug

* Update LocalSimpleCountRateCounter.java

bugfix

* fix:默认的nacos反脆弱插件的问题

* fix:默认的nacos反脆弱插件的问题

* fix:默认的nacos反脆弱插件的问题

* fix:默认的nacos反脆弱插件的问题

* fix:默认的nacos反脆弱插件的问题,调整为tryAdd

* fix:默认的nacos反脆弱插件的问题,调整为tryAdd

* 调整测试方式,保障流水线成功
This commit is contained in:
温安适 2024-02-22 11:32:04 +08:00 committed by GitHub
parent 1d3f1bb492
commit 1710fa32a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 8 deletions

View File

@ -50,7 +50,7 @@ public class LocalSimpleCountRateCounter extends RateCounter {
startTime = RateCounter.getTrimMillsOfHour(now);
} else {
//second default
getTrimMillsOfSecond(now);
startTime = RateCounter.getTrimMillsOfSecond(now);
}
}
@ -59,6 +59,16 @@ public class LocalSimpleCountRateCounter extends RateCounter {
return createSlotIfAbsent(timestamp).countHolder.count.addAndGet(count);
}
@Override
public boolean tryAdd(long timestamp, long countDelta, long upperLimit) {
if (createSlotIfAbsent(timestamp).countHolder.count.addAndGet(countDelta) <= upperLimit) {
return true;
} else {
createSlotIfAbsent(timestamp).countHolder.interceptedCount.addAndGet(countDelta);
return false;
}
}
public void minus(long timestamp, long count) {
AtomicLong currentCount = createSlotIfAbsent(timestamp).countHolder.count;
currentCount.addAndGet(count * -1);

View File

@ -53,7 +53,17 @@ public abstract class RateCounter {
* @return
*/
public abstract long add(long timestamp, long count);
/**
* add intercepted count for the second of timestamp.
*
* @param timestamp timestamp
* @param countDelta count
* @param upperLimit upperLimit
* @return
*/
public abstract boolean tryAdd(long timestamp, long countDelta, long upperLimit);
/**
* get count of the second of timestamp.
*

View File

@ -58,13 +58,15 @@ public abstract class SimpleCountRuleBarrier extends RuleBarrier {
@Override
public TpsCheckResponse applyTps(BarrierCheckRequest barrierCheckRequest) {
long count = rateCounter.getCount(barrierCheckRequest.getTimestamp());
long maxCount = getMaxCount();
if (MonitorType.INTERCEPT.getType().equals(getMonitorType()) && maxCount >= 0 && count >= maxCount) {
return new TpsCheckResponse(false, TpsResultCode.DENY_BY_POINT, "tps over limit :" + maxCount);
if (MonitorType.INTERCEPT.getType().equals(getMonitorType())) {
long maxCount = getMaxCount();
boolean accepted = rateCounter.tryAdd(barrierCheckRequest.getTimestamp(), barrierCheckRequest.getCount(), maxCount);
return accepted ? new TpsCheckResponse(true, TpsResultCode.PASS_BY_POINT, "success") :
new TpsCheckResponse(false, TpsResultCode.DENY_BY_POINT, "tps over limit :" + maxCount);
} else {
rateCounter.add(barrierCheckRequest.getTimestamp(), barrierCheckRequest.getCount());
return new TpsCheckResponse(true, TpsResultCode.PASS_BY_POINT, "success");
}
rateCounter.add(barrierCheckRequest.getTimestamp(), barrierCheckRequest.getCount());
return new TpsCheckResponse(true, TpsResultCode.PASS_BY_POINT, "success");
}
long trimTimeStamp(long timeStamp) {