Search in sources :

Example 1 with DestinationSet

use of com.tencent.polaris.client.pb.CircuitBreakerProto.DestinationSet in project polaris-java by polarismesh.

the class CircuitBreakUtils method matchDestination.

private static MatchDestResult matchDestination(CbRule rule, RuleIdentifier ruleIdentifier, FlowCache flowCache) {
    if (rule.getDestinationsCount() == 0) {
        return new MatchDestResult(null, false);
    }
    for (DestinationSet destinationSet : rule.getDestinationsList()) {
        boolean namespaceMatch = destinationSet.getNamespace().getValue().equals(matchAll);
        boolean serviceMatch = destinationSet.getService().getValue().equals(matchAll);
        if (!namespaceMatch) {
            namespaceMatch = destinationSet.getNamespace().getValue().equals(ruleIdentifier.getNamespace());
        }
        if (!serviceMatch) {
            serviceMatch = destinationSet.getService().getValue().equals(ruleIdentifier.getService());
        }
        if (!namespaceMatch || !serviceMatch) {
            continue;
        }
        MatchString methodMatcher = destinationSet.getMethod();
        if (null == methodMatcher) {
            return new MatchDestResult(destinationSet, true);
        }
        if (RuleUtils.isMatchAllValue(methodMatcher)) {
            return new MatchDestResult(destinationSet, true);
        }
        String method = ruleIdentifier.getMethod();
        if (methodMatcher.getType() == MatchStringType.EXACT) {
            if (StringUtils.equals(methodMatcher.getValue().getValue(), method)) {
                return new MatchDestResult(destinationSet, false);
            }
        }
        Pattern pattern = flowCache.loadOrStoreCompiledRegex(methodMatcher.getValue().getValue());
        if (pattern.matcher(method).find()) {
            return new MatchDestResult(destinationSet, false);
        }
    }
    return new MatchDestResult(null, false);
}
Also used : MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) Pattern(java.util.regex.Pattern) DestinationSet(com.tencent.polaris.client.pb.CircuitBreakerProto.DestinationSet) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString)

Aggregations

DestinationSet (com.tencent.polaris.client.pb.CircuitBreakerProto.DestinationSet)1 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)1 Pattern (java.util.regex.Pattern)1