Search in sources :

Example 1 with RateLimitReportRequest

use of com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitReportRequest in project polaris-java by polarismesh.

the class RemoteSyncTask method doRemoteAcquire.

/**
 * 上报
 */
private void doRemoteAcquire() {
    StreamCounterSet streamCounterSet = asyncRateLimitConnector.getStreamCounterSet(window.getWindowSet().getRateLimitExtension().getExtensions(), window.getRemoteCluster(), window.getUniqueKey(), serviceIdentifier);
    if (streamCounterSet == null) {
        LOG.error("[doRemoteAcquire] failed, stream counter is null. remote cluster:{},", window.getRemoteCluster());
        return;
    }
    if (!streamCounterSet.hasInit(serviceIdentifier)) {
        LOG.warn("[doRemoteAcquire] has not inited. serviceKey:{}", window.getSvcKey());
        doRemoteInit();
        return;
    }
    // 调整时间
    if (!adjustTime(streamCounterSet)) {
        LOG.error("[doRemoteAcquire] adjustTime failed.remote cluster:{},svcKey:{}", window.getRemoteCluster(), window.getSvcKey());
        return;
    }
    StreamObserver<RateLimitRequest> streamClient = streamCounterSet.preCheckAsync(serviceIdentifier, window);
    if (streamClient == null) {
        LOG.error("[doRemoteAcquire] failed, stream client is null. remote cluster:{}", window.getRemoteCluster());
        return;
    }
    RateLimitReportRequest.Builder rateLimitReportRequest = RateLimitReportRequest.newBuilder();
    // clientKey
    rateLimitReportRequest.setClientKey(streamCounterSet.getClientKey());
    // timestamp
    long curTimeMilli = System.currentTimeMillis();
    long serverTimeMilli = curTimeMilli + asyncRateLimitConnector.getTimeDiff().get();
    rateLimitReportRequest.setTimestamp(serverTimeMilli);
    // quotaUses
    Map<Integer, LocalQuotaInfo> localQuotaInfos = window.getAllocatingBucket().fetchLocalUsage(serverTimeMilli);
    for (Map.Entry<Integer, LocalQuotaInfo> entry : localQuotaInfos.entrySet()) {
        QuotaSum.Builder quotaSum = QuotaSum.newBuilder();
        quotaSum.setUsed((int) entry.getValue().getQuotaUsed());
        quotaSum.setLimited((int) entry.getValue().getQuotaLimited());
        quotaSum.setCounterKey(streamCounterSet.getInitRecord().get(serviceIdentifier).getDurationRecord().get(entry.getKey()));
        rateLimitReportRequest.addQuotaUses(quotaSum.build());
    }
    RateLimitRequest rateLimitRequest = RateLimitRequest.newBuilder().setCmd(RateLimitCmd.ACQUIRE).setRateLimitReportRequest(rateLimitReportRequest).build();
    streamClient.onNext(rateLimitRequest);
}
Also used : QuotaSum(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.QuotaSum) StreamCounterSet(com.tencent.polaris.ratelimit.client.flow.StreamCounterSet) RateLimitRequest(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest) RateLimitReportRequest(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitReportRequest) LocalQuotaInfo(com.tencent.polaris.api.plugin.ratelimiter.LocalQuotaInfo) Map(java.util.Map)

Aggregations

LocalQuotaInfo (com.tencent.polaris.api.plugin.ratelimiter.LocalQuotaInfo)1 StreamCounterSet (com.tencent.polaris.ratelimit.client.flow.StreamCounterSet)1 QuotaSum (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.QuotaSum)1 RateLimitReportRequest (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitReportRequest)1 RateLimitRequest (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest)1 Map (java.util.Map)1