Search in sources :

Example 6 with RateLimitRequest

use of com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest 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)

Example 7 with RateLimitRequest

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

the class RemoteSyncTask method doRemoteInit.

/**
 * 发送初始化请求
 */
private void doRemoteInit() {
    StreamCounterSet streamCounterSet = asyncRateLimitConnector.getStreamCounterSet(window.getWindowSet().getRateLimitExtension().getExtensions(), window.getRemoteCluster(), window.getUniqueKey(), serviceIdentifier);
    // 拿不到限流集群的实例的时候
    if (streamCounterSet == null) {
        LOG.error("[doRemoteInit] failed, stream counter is null. remote cluster:{},", window.getRemoteCluster());
        return;
    }
    // 调整时间
    if (!adjustTime(streamCounterSet)) {
        LOG.error("[doRemoteInit] adjustTime failed.remote cluster:{},svcKey:{}", window.getRemoteCluster(), window.getSvcKey());
        return;
    }
    StreamObserver<RateLimitRequest> streamClient = streamCounterSet.preCheckAsync(serviceIdentifier, window);
    if (streamClient == null) {
        LOG.error("[doRemoteInit] failed, stream client is null. remote cluster:{},svcKey:{}", window.getRemoteCluster(), window.getSvcKey());
        return;
    }
    // clientId
    Builder initRequest = RateLimitInitRequest.newBuilder();
    initRequest.setClientId(window.getWindowSet().getClientId());
    // target
    LimitTarget.Builder target = LimitTarget.newBuilder();
    target.setNamespace(window.getSvcKey().getNamespace());
    target.setService(window.getSvcKey().getService());
    target.setLabels(window.getLabels());
    initRequest.setTarget(target);
    // QuotaTotal
    QuotaMode quotaMode = QuotaMode.forNumber(window.getRule().getAmountModeValue());
    QuotaBucket allocatingBucket = window.getAllocatingBucket();
    Map<Integer, AmountInfo> amountInfos = allocatingBucket.getAmountInfo();
    if (MapUtils.isNotEmpty(amountInfos)) {
        for (Map.Entry<Integer, AmountInfo> entry : amountInfos.entrySet()) {
            QuotaTotal.Builder total = QuotaTotal.newBuilder();
            total.setDuration(entry.getKey());
            total.setMode(quotaMode);
            total.setMaxAmount((int) entry.getValue().getMaxAmount());
            initRequest.addTotals(total.build());
        }
    }
    RateLimitRequest rateLimitInitRequest = RateLimitRequest.newBuilder().setCmd(RateLimitCmd.INIT).setRateLimitInitRequest(initRequest).build();
    streamClient.onNext(rateLimitInitRequest);
}
Also used : Builder(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitInitRequest.Builder) StreamCounterSet(com.tencent.polaris.ratelimit.client.flow.StreamCounterSet) QuotaMode(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.QuotaMode) RateLimitRequest(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest) LimitTarget(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.LimitTarget) AmountInfo(com.tencent.polaris.api.plugin.ratelimiter.AmountInfo) Map(java.util.Map) QuotaTotal(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.QuotaTotal) QuotaBucket(com.tencent.polaris.api.plugin.ratelimiter.QuotaBucket)

Aggregations

TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)4 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)4 Test (org.junit.Test)4 RateLimitRequest (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest)3 TokenResultStatus (com.alibaba.csp.sentinel.cluster.TokenResultStatus)2 Tuple2 (com.alibaba.csp.sentinel.util.function.Tuple2)2 StreamCounterSet (com.tencent.polaris.ratelimit.client.flow.StreamCounterSet)2 RateLimitDescriptor (io.envoyproxy.envoy.api.v2.ratelimit.RateLimitDescriptor)2 RateLimitDescriptor (io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor)2 RateLimitRequest (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitRequest)2 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse)2 RateLimitRequest (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitRequest)2 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse)2 StreamObserver (io.grpc.stub.StreamObserver)2 Map (java.util.Map)2 Assert (org.junit.Assert)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 Mockito (org.mockito.Mockito)2 AmountInfo (com.tencent.polaris.api.plugin.ratelimiter.AmountInfo)1 LocalQuotaInfo (com.tencent.polaris.api.plugin.ratelimiter.LocalQuotaInfo)1