use of com.tencent.polaris.client.pb.RateLimitProto.RateLimit 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.RateLimit 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.RateLimit in project polaris-java by polarismesh.
the class NamingService method buildServiceResponse.
private ResponseProto.DiscoverResponse buildServiceResponse(int code, String info, DiscoverRequest req) {
ResponseProto.DiscoverResponse.Builder builder = ResponseProto.DiscoverResponse.newBuilder();
builder.setCode(UInt32Value.newBuilder().setValue(code).build());
CircuitBreakerProto.CircuitBreaker circuitBreaker;
RateLimitProto.RateLimit rateLimit;
RoutingProto.Routing routing;
List<ServiceProto.Instance> instances;
ServiceProto.Service service = req.getService();
ServiceKey serviceKey = new ServiceKey(service.getNamespace().getValue(), service.getName().getValue());
switch(req.getType()) {
case UNKNOWN:
break;
case INSTANCE:
instances = services.get(serviceKey);
if (CollectionUtils.isNotEmpty(instances)) {
builder.addAllInstances(instances);
}
builder.setType(DiscoverResponseType.INSTANCE);
break;
case CLUSTER:
break;
case ROUTING:
routing = serviceRoutings.get(serviceKey);
if (null != routing) {
builder.setRouting(routing);
}
builder.setType(DiscoverResponseType.ROUTING);
break;
case CIRCUIT_BREAKER:
circuitBreaker = serviceCircuitBreakers.get(serviceKey);
if (null != circuitBreaker) {
builder.setCircuitBreaker(circuitBreaker);
}
builder.setType(DiscoverResponseType.CIRCUIT_BREAKER);
break;
case RATE_LIMIT:
rateLimit = serviceRateLimits.get(serviceKey);
if (null != rateLimit) {
builder.setRateLimit(rateLimit);
}
builder.setType(DiscoverResponseType.RATE_LIMIT);
break;
case SERVICES:
Set<ServiceKey> keys = services.keySet();
Map<String, ServiceProto.Service> tmp = new HashMap<>();
String namespace = req.getService().getNamespace().getValue();
System.out.println("get service param : " + namespace);
keys.removeIf(serviceKey1 -> {
if (StringUtils.isBlank(namespace)) {
return false;
}
return !Objects.equals(namespace, serviceKey1.getNamespace());
});
keys.forEach(key -> {
tmp.put(key.getNamespace() + "##" + key.getService(), ServiceProto.Service.newBuilder().setNamespace(StringValue.newBuilder().setValue(key.getNamespace()).build()).setName(StringValue.newBuilder().setValue(key.getService()).build()).build());
});
final int[] index = { 0 };
tmp.forEach((s, svc) -> {
builder.addServices(index[0], svc);
index[0]++;
});
builder.setType(DiscoverResponseType.SERVICES);
break;
case UNRECOGNIZED:
break;
default:
break;
}
if (StringUtils.isNotBlank(info)) {
builder.setInfo(StringValue.newBuilder().setValue(info).build());
}
builder.setService(service);
return builder.build();
}
use of com.tencent.polaris.client.pb.RateLimitProto.RateLimit 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());
}
Aggregations