Search in sources :

Example 1 with LimitTarget

use of com.tencent.polaris.ratelimit.client.pb.RatelimitV2.LimitTarget 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());
}
Also used : QuotaCounter(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.QuotaCounter) RemoteQuotaInfo(com.tencent.polaris.api.plugin.ratelimiter.RemoteQuotaInfo) LimitTarget(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.LimitTarget)

Aggregations

RemoteQuotaInfo (com.tencent.polaris.api.plugin.ratelimiter.RemoteQuotaInfo)1 LimitTarget (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.LimitTarget)1 QuotaCounter (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.QuotaCounter)1