Search in sources :

Example 1 with FlowCache

use of com.tencent.polaris.api.plugin.cache.FlowCache in project polaris-java by polarismesh.

the class QuotaFlow method matchLabels.

private boolean matchLabels(String ruleLabelKey, MatchString ruleLabelMatch, Map<String, String> labels) {
    // 设置了MatchAllValue,相当于这个规则就无效了
    if (RuleUtils.isMatchAllValue(ruleLabelMatch)) {
        return true;
    }
    if (MapUtils.isEmpty(labels)) {
        return false;
    }
    // 集成的路由规则不包含这个key,就不匹配
    if (!labels.containsKey(ruleLabelKey)) {
        return false;
    }
    String labelValue = labels.get(ruleLabelKey);
    FlowCache flowCache = rateLimitExtension.getExtensions().getFlowCache();
    MatchStringType matchType = ruleLabelMatch.getType();
    String matchValue = ruleLabelMatch.getValue().getValue();
    if (matchType == MatchStringType.REGEX) {
        // 正则表达式匹配
        Pattern pattern = flowCache.loadOrStoreCompiledRegex(matchValue);
        return pattern.matcher(labelValue).find();
    }
    return StringUtils.equals(labelValue, matchValue);
}
Also used : MatchStringType(com.tencent.polaris.client.pb.ModelProto.MatchString.MatchStringType) Pattern(java.util.regex.Pattern) FlowCache(com.tencent.polaris.api.plugin.cache.FlowCache) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString)

Aggregations

FlowCache (com.tencent.polaris.api.plugin.cache.FlowCache)1 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)1 MatchStringType (com.tencent.polaris.client.pb.ModelProto.MatchString.MatchStringType)1 Pattern (java.util.regex.Pattern)1