diff --git a/polaris-examples/ratelimit-example/local-limiter-example/ratelimit-local-provider/src/main/java/com/tencent/polaris/ratelimit/local/example/Provider.java b/polaris-examples/ratelimit-example/local-limiter-example/ratelimit-local-provider/src/main/java/com/tencent/polaris/ratelimit/local/example/Provider.java index 97efd79f0..dbb29bfd1 100644 --- a/polaris-examples/ratelimit-example/local-limiter-example/ratelimit-local-provider/src/main/java/com/tencent/polaris/ratelimit/local/example/Provider.java +++ b/polaris-examples/ratelimit-example/local-limiter-example/ratelimit-local-provider/src/main/java/com/tencent/polaris/ratelimit/local/example/Provider.java @@ -30,7 +30,7 @@ import com.tencent.polaris.factory.ConfigAPIFactory; import com.tencent.polaris.factory.api.DiscoveryAPIFactory; import com.tencent.polaris.ratelimit.api.core.LimitAPI; -import com.tencent.polaris.ratelimit.api.rpc.MatchArgument; +import com.tencent.polaris.ratelimit.api.rpc.Argument; import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest; import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse; import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode; @@ -224,9 +224,9 @@ public void handle(HttpExchange exchange) throws IOException { quotaRequest.setNamespace(NAMESPACE_DEFAULT); quotaRequest.setService(ECHO_SERVICE_NAME); quotaRequest.setMethod("/echo"); - Set matchArgumentSet = new HashSet<>(); + Set matchArgumentSet = new HashSet<>(); for (Map.Entry entry : parameters.entrySet()) { - MatchArgument matchArgument = MatchArgument.buildQuery(entry.getKey(), entry.getValue()); + Argument matchArgument = Argument.buildQuery(entry.getKey(), entry.getValue()); matchArgumentSet.add(matchArgument); } quotaRequest.setArguments(matchArgumentSet); diff --git a/polaris-ratelimit/polaris-ratelimit-api/src/main/java/com/tencent/polaris/ratelimit/api/rpc/MatchArgument.java b/polaris-ratelimit/polaris-ratelimit-api/src/main/java/com/tencent/polaris/ratelimit/api/rpc/Argument.java similarity index 72% rename from polaris-ratelimit/polaris-ratelimit-api/src/main/java/com/tencent/polaris/ratelimit/api/rpc/MatchArgument.java rename to polaris-ratelimit/polaris-ratelimit-api/src/main/java/com/tencent/polaris/ratelimit/api/rpc/Argument.java index 35c739758..44cc8505c 100644 --- a/polaris-ratelimit/polaris-ratelimit-api/src/main/java/com/tencent/polaris/ratelimit/api/rpc/MatchArgument.java +++ b/polaris-ratelimit/polaris-ratelimit-api/src/main/java/com/tencent/polaris/ratelimit/api/rpc/Argument.java @@ -21,7 +21,7 @@ import java.util.Map; import java.util.Objects; -public class MatchArgument { +public class Argument { public enum ArgumentType { CUSTOM, METHOD, HEADER, QUERY, CALLER_SERVICE, CALLER_IP @@ -33,7 +33,7 @@ public enum ArgumentType { private final String value; - private MatchArgument(ArgumentType type, String key, String value) { + private Argument(ArgumentType type, String key, String value) { this.type = type; this.key = key; this.value = value; @@ -51,37 +51,37 @@ public String getValue() { return value; } - public static MatchArgument buildCustom(String key, String value) { - return new MatchArgument(ArgumentType.CUSTOM, StringUtils.defaultString(key), StringUtils.defaultString(value)); + public static Argument buildCustom(String key, String value) { + return new Argument(ArgumentType.CUSTOM, StringUtils.defaultString(key), StringUtils.defaultString(value)); } - public static MatchArgument buildMethod(String method) { - return new MatchArgument(ArgumentType.METHOD, "", StringUtils.defaultString(method)); + public static Argument buildMethod(String method) { + return new Argument(ArgumentType.METHOD, "", StringUtils.defaultString(method)); } - public static MatchArgument buildHeader(String headerKey, String headerValue) { - return new MatchArgument(ArgumentType.HEADER, StringUtils.defaultString(headerKey), + public static Argument buildHeader(String headerKey, String headerValue) { + return new Argument(ArgumentType.HEADER, StringUtils.defaultString(headerKey), StringUtils.defaultString(headerValue)); } - public static MatchArgument buildQuery(String queryKey, String queryValue) { - return new MatchArgument(ArgumentType.QUERY, StringUtils.defaultString(queryKey), + public static Argument buildQuery(String queryKey, String queryValue) { + return new Argument(ArgumentType.QUERY, StringUtils.defaultString(queryKey), StringUtils.defaultString(queryValue)); } - public static MatchArgument buildCallerService(String namespace, String service) { - return new MatchArgument(ArgumentType.CALLER_SERVICE, StringUtils.defaultString(namespace), + public static Argument buildCallerService(String namespace, String service) { + return new Argument(ArgumentType.CALLER_SERVICE, StringUtils.defaultString(namespace), StringUtils.defaultString(service)); } - public static MatchArgument buildCallerIP(String callerIP) { - return new MatchArgument(ArgumentType.CALLER_IP, "", StringUtils.defaultString(callerIP)); + public static Argument buildCallerIP(String callerIP) { + return new Argument(ArgumentType.CALLER_IP, "", StringUtils.defaultString(callerIP)); } - public static MatchArgument fromLabel(String labelKey, String labelValue) { + public static Argument fromLabel(String labelKey, String labelValue) { labelKey = StringUtils.defaultString(labelKey); if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_METHOD)) { - return buildMethod(labelKey); + return buildMethod(labelValue); } if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_CALLER_IP)) { return buildCallerIP(labelValue); @@ -135,10 +135,10 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof MatchArgument)) { + if (!(o instanceof Argument)) { return false; } - MatchArgument that = (MatchArgument) o; + Argument that = (Argument) o; return type == that.type && Objects.equals(key, that.key) && Objects.equals(value, that.value); diff --git a/polaris-ratelimit/polaris-ratelimit-api/src/main/java/com/tencent/polaris/ratelimit/api/rpc/QuotaRequest.java b/polaris-ratelimit/polaris-ratelimit-api/src/main/java/com/tencent/polaris/ratelimit/api/rpc/QuotaRequest.java index bc48a3b2b..bad15dd19 100644 --- a/polaris-ratelimit/polaris-ratelimit-api/src/main/java/com/tencent/polaris/ratelimit/api/rpc/QuotaRequest.java +++ b/polaris-ratelimit/polaris-ratelimit-api/src/main/java/com/tencent/polaris/ratelimit/api/rpc/QuotaRequest.java @@ -34,7 +34,7 @@ public class QuotaRequest extends RequestBaseEntity { private String method; - private Set arguments = new HashSet<>(); + private Set arguments = new HashSet<>(); private int count = 1; @@ -62,7 +62,7 @@ public void setService(String service) { @Deprecated public Map getLabels() { Map values = new HashMap<>(); - for (MatchArgument matchArgument : arguments) { + for (Argument matchArgument : arguments) { matchArgument.toLabel(values); } return values; @@ -77,7 +77,7 @@ public void setLabels(Map labels) { return; } for (Map.Entry entry : labels.entrySet()) { - arguments.add(MatchArgument.fromLabel(entry.getKey(), entry.getValue())); + arguments.add(Argument.fromLabel(entry.getKey(), entry.getValue())); } } @@ -97,11 +97,11 @@ public void setMethod(String method) { this.method = method; } - public Set getArguments() { + public Set getArguments() { return arguments; } - public void setArguments(Set arguments) { + public void setArguments(Set arguments) { if (CollectionUtils.isEmpty(arguments)) { this.arguments = Collections.emptySet(); } else { diff --git a/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/codec/RateLimitingCacheHandler.java b/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/codec/RateLimitingCacheHandler.java index f42feba20..43faa48f6 100644 --- a/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/codec/RateLimitingCacheHandler.java +++ b/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/codec/RateLimitingCacheHandler.java @@ -22,6 +22,7 @@ import com.tencent.polaris.api.pojo.RegistryCacheValue; import com.tencent.polaris.api.pojo.ServiceEventKey.EventType; import com.tencent.polaris.api.utils.CollectionUtils; +import com.tencent.polaris.api.utils.RuleUtils; import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.client.pb.ModelProto.MatchString; import com.tencent.polaris.client.pb.RateLimitProto.MatchArgument; @@ -78,7 +79,7 @@ private static int getRuleLevel(Rule rule) { if (CollectionUtils.isNotEmpty(argumentsList)) { return RULE_ARGUMENT_LEVEL + argumentsList.size(); } - if (null != method) { + if (null != method && !RuleUtils.isMatchAllValue(method)) { return RULE_METHOD_LEVEL; } return RULE_SERVICE_LEVEL; @@ -112,23 +113,23 @@ private List unifiedRules(List rules) { // transfer the labels to arguments List arguments = new ArrayList<>(); for (Map.Entry entry : rule.getLabelsMap().entrySet()) { - String labelKey = entry.getKey(); + String labelKey = StringUtils.defaultString(entry.getKey()); if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_METHOD)) { arguments.add(MatchArgument.newBuilder().setType(Type.METHOD).setValue(entry.getValue()).build()); } else if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_CALLER_IP)) { arguments .add(MatchArgument.newBuilder().setType(Type.CALLER_IP).setValue(entry.getValue()).build()); - } else if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_HEADER)) { + } else if (labelKey.startsWith(RateLimitConsts.LABEL_KEY_HEADER)) { arguments .add(MatchArgument.newBuilder().setType(Type.HEADER) .setKey(labelKey.substring(RateLimitConsts.LABEL_KEY_HEADER.length())) .setValue(entry.getValue()).build()); - } else if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_QUERY)) { + } else if (labelKey.startsWith(RateLimitConsts.LABEL_KEY_QUERY)) { arguments .add(MatchArgument.newBuilder().setType(Type.QUERY) .setKey(labelKey.substring(RateLimitConsts.LABEL_KEY_QUERY.length())) .setValue(entry.getValue()).build()); - } else if (StringUtils.equals(labelKey, RateLimitConsts.LABEL_KEY_CALLER_SERVICE)) { + } else if (labelKey.startsWith(RateLimitConsts.LABEL_KEY_CALLER_SERVICE)) { arguments .add(MatchArgument.newBuilder().setType(Type.CALLER_SERVICE) .setKey(labelKey.substring(RateLimitConsts.LABEL_KEY_CALLER_SERVICE.length())) diff --git a/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/flow/QuotaFlow.java b/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/flow/QuotaFlow.java index b2b75c25d..1ddc22f70 100644 --- a/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/flow/QuotaFlow.java +++ b/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/flow/QuotaFlow.java @@ -40,7 +40,6 @@ import com.tencent.polaris.client.pb.ModelProto.MatchString; import com.tencent.polaris.client.pb.ModelProto.MatchString.MatchStringType; import com.tencent.polaris.client.pb.RateLimitProto; -import com.tencent.polaris.client.pb.RateLimitProto.MatchArgument; import com.tencent.polaris.client.pb.RateLimitProto.RateLimit; import com.tencent.polaris.client.pb.RateLimitProto.Rule; import com.tencent.polaris.logging.LoggerFactory; @@ -167,10 +166,10 @@ private static String formatLabelsToStr(CommonQuotaRequest request, InitCriteria } } } - List argumentsList = rule.getArgumentsList(); + List argumentsList = rule.getArgumentsList(); List tmpList = new ArrayList<>(); Map> arguments = request.getArguments(); - for (MatchArgument matchArgument : argumentsList) { + for (RateLimitProto.MatchArgument matchArgument : argumentsList) { String labelValue; MatchString matcher = matchArgument.getValue(); if (regexCombine && matcher.getType() != MatchStringType.EXACT) { diff --git a/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/pojo/CommonQuotaRequest.java b/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/pojo/CommonQuotaRequest.java index c25f7da49..0b66d2ac2 100644 --- a/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/pojo/CommonQuotaRequest.java +++ b/polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/pojo/CommonQuotaRequest.java @@ -27,7 +27,7 @@ import com.tencent.polaris.client.flow.BaseFlow; import com.tencent.polaris.client.flow.DefaultFlowControlParam; import com.tencent.polaris.client.flow.FlowControlParam; -import com.tencent.polaris.ratelimit.api.rpc.MatchArgument; +import com.tencent.polaris.ratelimit.api.rpc.Argument; import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest; import java.util.Collection; import java.util.HashMap; @@ -59,12 +59,12 @@ public CommonQuotaRequest(QuotaRequest quotaRequest, Configuration configuration BaseFlow.buildFlowControlParam(quotaRequest, configuration, flowControlParam); } - private Map> parseArguments(Collection arguments) { + private Map> parseArguments(Collection arguments) { Map> argumentMap = new HashMap<>(); if (CollectionUtils.isEmpty(arguments)) { return argumentMap; } - for (MatchArgument argument : arguments) { + for (Argument argument : arguments) { Map stringMatchArgumentMap = argumentMap .computeIfAbsent(argument.getType().ordinal(), k -> new HashMap<>()); stringMatchArgumentMap.put(argument.getKey(), argument.getValue()); diff --git a/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/LocalTest.java b/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/LocalTest.java index 2f82d49a8..965c41a77 100644 --- a/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/LocalTest.java +++ b/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/LocalTest.java @@ -34,6 +34,7 @@ import com.tencent.polaris.client.pb.RateLimitProto.Rule.Type; import com.tencent.polaris.client.util.Utils; import com.tencent.polaris.ratelimit.api.core.LimitAPI; +import com.tencent.polaris.ratelimit.api.rpc.Argument; import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest; import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse; import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode; @@ -110,9 +111,9 @@ private static void testQuotaAcquire(LimitAPI limitAPI, Map labe QuotaRequest payRequest = new QuotaRequest(); payRequest.setNamespace(Consts.NAMESPACE_TEST); payRequest.setService(Consts.LOCAL_LIMIT_SERVICE); - Set matchArgumentSet = new HashSet<>(); + Set matchArgumentSet = new HashSet<>(); for (Map.Entry entry : labels.entrySet()) { - com.tencent.polaris.ratelimit.api.rpc.MatchArgument matchArgument = com.tencent.polaris.ratelimit.api.rpc.MatchArgument + Argument matchArgument = Argument .buildCustom(entry.getKey(), entry.getValue()); matchArgumentSet.add(matchArgument); } diff --git a/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/MultiRuleTest.java b/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/MultiRuleTest.java index 7c488e7bc..048c85ec4 100644 --- a/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/MultiRuleTest.java +++ b/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/MultiRuleTest.java @@ -33,6 +33,7 @@ import com.tencent.polaris.client.pb.RateLimitProto.Rule.Type; import com.tencent.polaris.client.util.Utils; import com.tencent.polaris.ratelimit.api.core.LimitAPI; +import com.tencent.polaris.ratelimit.api.rpc.Argument; import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest; import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse; import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode; @@ -135,8 +136,8 @@ private QuotaResponse quotaAcquire(LimitAPI limitAPI, String method, String head payRequest.setMethod(method); } if (null != headerValue) { - Set matchArguments = new HashSet<>(); - matchArguments.add(com.tencent.polaris.ratelimit.api.rpc.MatchArgument + Set matchArguments = new HashSet<>(); + matchArguments.add(Argument .buildHeader(Consts.HEADER_KEY, headerValue)); payRequest.setArguments(matchArguments); } diff --git a/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/RuleMatchTest.java b/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/RuleMatchTest.java index a5d84d374..ca99b4169 100644 --- a/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/RuleMatchTest.java +++ b/polaris-ratelimit/polaris-ratelimit-factory/src/test/java/com/tencent/polaris/ratelimit/test/core/RuleMatchTest.java @@ -33,6 +33,7 @@ import com.tencent.polaris.client.pb.RateLimitProto.Rule.Type; import com.tencent.polaris.client.util.Utils; import com.tencent.polaris.ratelimit.api.core.LimitAPI; +import com.tencent.polaris.ratelimit.api.rpc.Argument; import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest; import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse; import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode; @@ -160,8 +161,8 @@ private QuotaResponse quotaAcquire(LimitAPI limitAPI, String service, String met payRequest.setMethod(method); } if (null != headerValue) { - Set matchArguments = new HashSet<>(); - matchArguments.add(com.tencent.polaris.ratelimit.api.rpc.MatchArgument + Set matchArguments = new HashSet<>(); + matchArguments.add(Argument .buildHeader(Consts.HEADER_KEY, headerValue)); payRequest.setArguments(matchArguments); }