* 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);
|
startTime = RateCounter.getTrimMillsOfHour(now);
|
||||||
} else {
|
} else {
|
||||||
//second default
|
//second default
|
||||||
getTrimMillsOfSecond(now);
|
startTime = RateCounter.getTrimMillsOfSecond(now);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,6 +59,16 @@ public class LocalSimpleCountRateCounter extends RateCounter {
|
|||||||
return createSlotIfAbsent(timestamp).countHolder.count.addAndGet(count);
|
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) {
|
public void minus(long timestamp, long count) {
|
||||||
AtomicLong currentCount = createSlotIfAbsent(timestamp).countHolder.count;
|
AtomicLong currentCount = createSlotIfAbsent(timestamp).countHolder.count;
|
||||||
currentCount.addAndGet(count * -1);
|
currentCount.addAndGet(count * -1);
|
||||||
|
@ -54,6 +54,16 @@ public abstract class RateCounter {
|
|||||||
*/
|
*/
|
||||||
public abstract long add(long timestamp, long count);
|
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.
|
* get count of the second of timestamp.
|
||||||
*
|
*
|
||||||
|
@ -58,14 +58,16 @@ public abstract class SimpleCountRuleBarrier extends RuleBarrier {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TpsCheckResponse applyTps(BarrierCheckRequest barrierCheckRequest) {
|
public TpsCheckResponse applyTps(BarrierCheckRequest barrierCheckRequest) {
|
||||||
long count = rateCounter.getCount(barrierCheckRequest.getTimestamp());
|
if (MonitorType.INTERCEPT.getType().equals(getMonitorType())) {
|
||||||
long maxCount = getMaxCount();
|
long maxCount = getMaxCount();
|
||||||
if (MonitorType.INTERCEPT.getType().equals(getMonitorType()) && maxCount >= 0 && count >= maxCount) {
|
boolean accepted = rateCounter.tryAdd(barrierCheckRequest.getTimestamp(), barrierCheckRequest.getCount(), maxCount);
|
||||||
return new TpsCheckResponse(false, TpsResultCode.DENY_BY_POINT, "tps over limit :" + 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());
|
rateCounter.add(barrierCheckRequest.getTimestamp(), barrierCheckRequest.getCount());
|
||||||
return new TpsCheckResponse(true, TpsResultCode.PASS_BY_POINT, "success");
|
return new TpsCheckResponse(true, TpsResultCode.PASS_BY_POINT, "success");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
long trimTimeStamp(long timeStamp) {
|
long trimTimeStamp(long timeStamp) {
|
||||||
if (this.getPeriod() == TimeUnit.SECONDS) {
|
if (this.getPeriod() == TimeUnit.SECONDS) {
|
||||||
|
Loading…
Reference in New Issue
Block a user