use of com.tencent.polaris.api.plugin.ratelimiter.RemoteQuotaInfo in project polaris-java by polarismesh.
the class RemoteAwareBucket method onRemoteUpdate.
@Override
public void onRemoteUpdate(RemoteQuotaInfo remoteQuotaInfo) {
long localCurTimeMs = System.currentTimeMillis();
long remoteCurTimeMs = remoteQuotaInfo.getCurTimeMs();
long durationMs = remoteQuotaInfo.getDurationMs();
long localCurStartMs = SlidingWindow.calculateStartTimeMs(localCurTimeMs, durationMs);
long remoteCurStartMs = SlidingWindow.calculateStartTimeMs(remoteCurTimeMs, durationMs);
TokenBucket tokenBucket = tokenBucketMap.get(durationMs);
if (null == tokenBucket) {
return;
}
if (remoteCurStartMs != localCurStartMs) {
long remoteQuotaLeft = remoteQuotaInfo.getRemoteQuotaLeft();
if (remoteCurStartMs + durationMs == localCurStartMs) {
// 仅仅相差一个周期,可以认为是周期间切换导致,这时候可以直接更新配额为全量配额
// 当前周期没有更新,则重置当前周期配额,避免出现时间周期开始时候的误限
remoteQuotaInfo = new RemoteQuotaInfo(tokenBucket.getRuleTotal(), remoteQuotaInfo.getClientCount(), localCurStartMs, durationMs);
LOG.warn("[RateLimit]reset remote quota, localTimeMilli {}(startMilli {}), " + "remoteTimeMilli {}(startMilli {}), interval {}, remoteLeft is {}, reset to {}", localCurTimeMs, localCurStartMs, remoteCurTimeMs, remoteCurStartMs, durationMs, remoteQuotaLeft, remoteQuotaInfo.getRemoteQuotaLeft());
} else {
tokenBucket.syncUpdateRemoteClientCount(remoteQuotaInfo);
// 不在一个时间段内,丢弃
LOG.warn("[RateLimit]Drop remote quota, localTimeMilli {}(startMilli {}), " + "remoteTimeMilli {}(startMilli {}), interval {}, remoteLeft is {}", localCurTimeMs, localCurStartMs, remoteCurTimeMs, remoteCurStartMs, durationMs, remoteQuotaLeft);
}
}
tokenBucket.syncUpdateRemoteToken(remoteQuotaInfo);
}
use of com.tencent.polaris.api.plugin.ratelimiter.RemoteQuotaInfo in project polaris-java by polarismesh.
the class StreamCounterSet method handleRateLimitInitResponse.
/**
* 处理初始化请求的response
*
* @param rateLimitInitResponse 初始化请求的返回结果
*/
void handleRateLimitInitResponse(RateLimitInitResponse rateLimitInitResponse) {
LOG.debug("[handleRateLimitInitResponse] response:{}", rateLimitInitResponse);
if (rateLimitInitResponse.getCode() != RateLimitConstants.SUCCESS) {
LOG.error("[handleRateLimitInitResponse] failed. code is {}", rateLimitInitResponse.getCode());
return;
}
LimitTarget target = rateLimitInitResponse.getTarget();
ServiceIdentifier serviceIdentifier = new ServiceIdentifier(target.getService(), target.getNamespace(), target.getLabels());
InitializeRecord initializeRecord = initRecord.get(serviceIdentifier);
if (initializeRecord == null) {
LOG.error("[handleRateLimitInitResponse] can not find init record:{}", serviceIdentifier);
return;
}
// client key
setClientKey(rateLimitInitResponse.getClientKey());
List<QuotaCounter> countersList = rateLimitInitResponse.getCountersList();
if (CollectionUtils.isEmpty(countersList)) {
LOG.error("[handleRateLimitInitResponse] countersList is empty.");
return;
}
// 重新初始化后,之前的记录就不要了
initializeRecord.getDurationRecord().clear();
long serverTimeMilli = rateLimitInitResponse.getTimestamp() + asyncConnector.getTimeDiff().get();
countersList.forEach(counter -> {
initializeRecord.getDurationRecord().putIfAbsent(counter.getDuration(), counter.getCounterKey());
getCounters().putIfAbsent(counter.getCounterKey(), new DurationBaseCallback(counter.getDuration(), initializeRecord.getRateLimitWindow()));
RemoteQuotaInfo remoteQuotaInfo = new RemoteQuotaInfo(counter.getLeft(), counter.getClientCount(), serverTimeMilli, counter.getDuration() * 1000);
initializeRecord.getRateLimitWindow().getAllocatingBucket().onRemoteUpdate(remoteQuotaInfo);
});
initializeRecord.getRateLimitWindow().setStatus(WindowStatus.INITIALIZED.ordinal());
}
use of com.tencent.polaris.api.plugin.ratelimiter.RemoteQuotaInfo in project polaris-java by polarismesh.
the class StreamCounterSet method handleRateLimitReportResponse.
/**
* 处理acquire的回包
*
* @param rateLimitReportResponse report的回包
*/
void handleRateLimitReportResponse(RateLimitReportResponse rateLimitReportResponse) {
LOG.debug("[handleRateLimitReportRequest] response:{}", rateLimitReportResponse);
if (rateLimitReportResponse.getCode() != RateLimitConstants.SUCCESS) {
LOG.error("[handleRateLimitReportRequest] failed. code is {}", rateLimitReportResponse.getCode());
return;
}
long serverTimeMilli = rateLimitReportResponse.getTimestamp();
List<QuotaLeft> quotaLeftsList = rateLimitReportResponse.getQuotaLeftsList();
if (CollectionUtils.isEmpty(quotaLeftsList)) {
LOG.error("[handleRateLimitReportRequest] quotaLefts is empty.");
return;
}
quotaLeftsList.forEach(quotaLeft -> {
DurationBaseCallback callback = getCounters().get(quotaLeft.getCounterKey());
RemoteQuotaInfo remoteQuotaInfo = new RemoteQuotaInfo(quotaLeft.getLeft(), quotaLeft.getClientCount(), serverTimeMilli, callback.getDuration() * 1000);
callback.getRateLimitWindow().getAllocatingBucket().onRemoteUpdate(remoteQuotaInfo);
});
}
Aggregations