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;
}
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;
}
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());
}
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);
}
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);
}
Aggregations