Search in sources :

Example 1 with Rule

use of com.tencent.polaris.client.pb.RateLimitProto.Rule in project polaris-java by polarismesh.

the class QuotaFlow method parseRules.

private static Map<String, Rule> parseRules(RegistryCacheValue oldValue) {
    if (null == oldValue || !oldValue.isInitialized()) {
        return null;
    }
    ServiceRule serviceRule = (ServiceRule) oldValue;
    if (null == serviceRule.getRule()) {
        return null;
    }
    Map<String, Rule> ruleMap = new HashMap<>();
    RateLimit rateLimit = (RateLimit) serviceRule.getRule();
    for (Rule rule : rateLimit.getRulesList()) {
        ruleMap.put(rule.getRevision().getValue(), rule);
    }
    return ruleMap;
}
Also used : RateLimit(com.tencent.polaris.client.pb.RateLimitProto.RateLimit) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule)

Example 2 with Rule

use of com.tencent.polaris.client.pb.RateLimitProto.Rule in project polaris-java by polarismesh.

the class QuotaFlow method lookupRule.

private Rule lookupRule(ServiceRule serviceRule, Map<String, String> labels) {
    if (null == serviceRule.getRule()) {
        return null;
    }
    RateLimit rateLimitProto = (RateLimit) serviceRule.getRule();
    List<Rule> rulesList = rateLimitProto.getRulesList();
    if (CollectionUtils.isEmpty(rulesList)) {
        return null;
    }
    for (Rule rule : rulesList) {
        if (null != rule.getDisable() && rule.getDisable().getValue()) {
            continue;
        }
        if (rule.getAmountsCount() == 0) {
            // 没有amount的规则就忽略
            continue;
        }
        if (rule.getLabelsCount() == 0) {
            return rule;
        }
        boolean allMatchLabels = true;
        Map<String, MatchString> labelsMap = rule.getLabelsMap();
        for (Map.Entry<String, MatchString> entry : labelsMap.entrySet()) {
            if (!matchLabels(entry.getKey(), entry.getValue(), labels)) {
                allMatchLabels = false;
                break;
            }
        }
        if (allMatchLabels) {
            return rule;
        }
    }
    return null;
}
Also used : MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) RateLimit(com.tencent.polaris.client.pb.RateLimitProto.RateLimit) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with Rule

use of com.tencent.polaris.client.pb.RateLimitProto.Rule in project polaris-java by polarismesh.

the class RateLimitingCacheHandler method messageToCacheValue.

@Override
public RegistryCacheValue messageToCacheValue(RegistryCacheValue oldValue, Object newValue, boolean isCacheLoaded) {
    DiscoverResponse discoverResponse = (DiscoverResponse) newValue;
    RateLimit rateLimit = discoverResponse.getRateLimit();
    String revision = getRevision(discoverResponse);
    List<Rule> rulesList = rateLimit.getRulesList();
    // 需要做一次排序,PB中的数据不可变,需要单独构建一份
    List<Rule> sortedRules = new ArrayList<>(rulesList);
    sortedRules.sort((o1, o2) -> {
        if (o1.getPriority().getValue() != o2.getPriority().getValue()) {
            return o1.getPriority().getValue() - o2.getPriority().getValue();
        }
        return o1.getId().getValue().compareTo(o2.getId().getValue());
    });
    RateLimit newRateLimit = RateLimit.newBuilder().addAllRules(sortedRules).setRevision(StringValue.newBuilder().setValue(revision).build()).build();
    return new ServiceRuleByProto(newRateLimit, revision, isCacheLoaded, getTargetEventType());
}
Also used : RateLimit(com.tencent.polaris.client.pb.RateLimitProto.RateLimit) ArrayList(java.util.ArrayList) DiscoverResponse(com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule) ServiceRuleByProto(com.tencent.polaris.client.pojo.ServiceRuleByProto)

Example 4 with Rule

use of com.tencent.polaris.client.pb.RateLimitProto.Rule in project polaris-java by polarismesh.

the class RateLimitWindowSet method addRateLimitWindow.

/**
 * 增加限流窗口
 *
 * @param request 请求
 * @param labelsStr 标签
 * @return window
 */
public RateLimitWindow addRateLimitWindow(CommonQuotaRequest request, String labelsStr) {
    Rule targetRule = request.getInitCriteria().getRule();
    String revision = targetRule.getRevision().getValue();
    Function<String, RateLimitWindow> createRateLimitWindow = new Function<String, RateLimitWindow>() {

        @Override
        public RateLimitWindow apply(String label) {
            return new RateLimitWindow(RateLimitWindowSet.this, request, label);
        }
    };
    WindowContainer container = windowByRule.computeIfAbsent(revision, new Function<String, WindowContainer>() {

        @Override
        public WindowContainer apply(String s) {
            RateLimitWindow window = createRateLimitWindow.apply(labelsStr);
            return new WindowContainer(serviceKey, labelsStr, window, request.isRegexSpread());
        }
    });
    RateLimitWindow mainWindow = container.getLabelWindow(labelsStr);
    if (null != mainWindow) {
        return mainWindow;
    }
    return container.computeLabelWindow(labelsStr, createRateLimitWindow);
}
Also used : Function(java.util.function.Function) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule)

Example 5 with Rule

use of com.tencent.polaris.client.pb.RateLimitProto.Rule in project polaris-java by polarismesh.

the class QuotaFlow method formatLabelsToStr.

private static String formatLabelsToStr(CommonQuotaRequest request) {
    Rule rule = request.getInitCriteria().getRule();
    Map<String, String> labels = request.getLabels();
    if (rule.getLabelsCount() == 0 || MapUtils.isEmpty(labels)) {
        return "";
    }
    List<String> tmpList = new ArrayList<>();
    boolean regexCombine = rule.getRegexCombine().getValue();
    Map<String, MatchString> labelsMap = rule.getLabelsMap();
    for (Map.Entry<String, MatchString> entry : labelsMap.entrySet()) {
        MatchString matcher = entry.getValue();
        String labelEntry;
        if (matcher.getType() == MatchStringType.REGEX && regexCombine) {
            labelEntry = entry.getKey() + RateLimitConstants.DEFAULT_KV_SEPARATOR + matcher.getValue().getValue();
        } else {
            labelEntry = entry.getKey() + RateLimitConstants.DEFAULT_KV_SEPARATOR + labels.get(entry.getKey());
            if (matcher.getType() == MatchStringType.REGEX) {
                // 正则表达式扩散
                request.setRegexSpread(true);
            }
        }
        tmpList.add(labelEntry);
    }
    Collections.sort(tmpList);
    return String.join(RateLimitConstants.DEFAULT_ENTRY_SEPARATOR, tmpList);
}
Also used : MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) ArrayList(java.util.ArrayList) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Rule (com.tencent.polaris.client.pb.RateLimitProto.Rule)6 ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)4 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)4 RateLimit (com.tencent.polaris.client.pb.RateLimitProto.RateLimit)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 ResourcesResponse (com.tencent.polaris.client.flow.ResourcesResponse)1 DiscoverResponse (com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse)1 ServiceRuleByProto (com.tencent.polaris.client.pojo.ServiceRuleByProto)1 Function (java.util.function.Function)1