Search in sources :

Example 1 with QuotaMode

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

AmountInfo (com.tencent.polaris.api.plugin.ratelimiter.AmountInfo)1 QuotaBucket (com.tencent.polaris.api.plugin.ratelimiter.QuotaBucket)1 StreamCounterSet (com.tencent.polaris.ratelimit.client.flow.StreamCounterSet)1 LimitTarget (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.LimitTarget)1 QuotaMode (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.QuotaMode)1 QuotaTotal (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.QuotaTotal)1 Builder (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitInitRequest.Builder)1 RateLimitRequest (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest)1 Map (java.util.Map)1