* 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:
parent
1d3f1bb492
commit
1710fa32a2
@ -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);
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user