Skip to content

Commit e60f0d0

Browse files
authored
feat: skip regex resource matching when exact match exists (default disabled, configurable)(#3565)
* Simple rules should have higher priority than regular expression rules * Add optional switch to skip regex matching when simple rules already match, default: false, keeps backward compatibility. * fix RuleManagerTest unit test failure
1 parent d75a668 commit e60f0d0

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ public final class SentinelConfig {
6161
public static final String STATISTIC_MAX_RT = "csp.sentinel.statistic.max.rt";
6262
public static final String SPI_CLASSLOADER = "csp.sentinel.spi.classloader";
6363
public static final String METRIC_FLUSH_INTERVAL = "csp.sentinel.metric.flush.interval";
64+
public static final String SKIP_REGEX_IF_SIMPLE_RULE_MATCHED_KEY = "csp.sentinel.rule.regex.skip.if.simple.matched";
6465

6566
public static final String DEFAULT_CHARSET = "UTF-8";
6667
public static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50;
6768
public static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6;
6869
public static final int DEFAULT_COLD_FACTOR = 3;
6970
public static final int DEFAULT_STATISTIC_MAX_RT = 5000;
7071
public static final long DEFAULT_METRIC_FLUSH_INTERVAL = 1L;
72+
public static final String DEFAULT_SKIP_REGEX_IF_SIMPLE_RULE_MATCHED = "false";
7173

7274
static {
7375
try {
@@ -106,6 +108,7 @@ private static void initialize() {
106108
setConfig(COLD_FACTOR, String.valueOf(DEFAULT_COLD_FACTOR));
107109
setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT));
108110
setConfig(METRIC_FLUSH_INTERVAL, String.valueOf(DEFAULT_METRIC_FLUSH_INTERVAL));
111+
setConfig(SKIP_REGEX_IF_SIMPLE_RULE_MATCHED_KEY, DEFAULT_SKIP_REGEX_IF_SIMPLE_RULE_MATCHED);
109112
}
110113

111114
private static void loadProps() {
@@ -341,5 +344,13 @@ public static boolean shouldUseContextClassloader() {
341344
return CLASSLOADER_CONTEXT.equalsIgnoreCase(classloaderConf);
342345
}
343346

347+
/**
348+
* Return whether to skip regex matching when simple rules already matched.
349+
* Default: false (keeps backward compatibility).
350+
*/
351+
public static boolean shouldSkipRegexIfSimpleRuleMatched() {
352+
return Boolean.parseBoolean(getConfig(SKIP_REGEX_IF_SIMPLE_RULE_MATCHED_KEY));
353+
}
354+
344355
private SentinelConfig() {}
345356
}

sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/RuleManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.alibaba.csp.sentinel.util.function.Function;
1919
import com.alibaba.csp.sentinel.util.function.Predicate;
20+
import com.alibaba.csp.sentinel.config.SentinelConfig;
2021

2122
import java.util.*;
2223
import java.util.regex.Pattern;
@@ -86,7 +87,7 @@ public void updateRules(Map<String, List<R>> rulesMap) {
8687
*/
8788
public List<R> getRules(String resource) {
8889
List<R> result = new ArrayList<>(simpleRules.getOrDefault(resource, Collections.emptyList()));
89-
if (regexRules.isEmpty()) {
90+
if (regexRules.isEmpty() || (SentinelConfig.shouldSkipRegexIfSimpleRuleMatched() && !result.isEmpty())) {
9091
return result;
9192
}
9293
if (regexCacheRules.containsKey(resource)) {

sentinel-core/src/test/java/com/alibaba/csp/sentinel/config/SentinelConfigTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public void testDefaultConfig() {
2626
assertEquals(SentinelConfig.DEFAULT_TOTAL_METRIC_FILE_COUNT, SentinelConfig.totalMetricFileCount());
2727
assertEquals(SentinelConfig.DEFAULT_COLD_FACTOR, SentinelConfig.coldFactor());
2828
assertEquals(SentinelConfig.DEFAULT_STATISTIC_MAX_RT, SentinelConfig.statisticMaxRt());
29+
assertEquals(false, SentinelConfig.shouldSkipRegexIfSimpleRuleMatched());
2930
}
3031

3132
// add JVM parameter

sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/RuleManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void testGetRulesWithCache() throws Exception {
5151
Field regexCacheRules = RuleManager.class.getDeclaredField("regexCacheRules");
5252
regexCacheRules.setAccessible(true);
5353
assertEquals(((Map)regexCacheRules.get(ruleManager)).size(), 0);
54-
ruleManager.getRules("rule2");
54+
ruleManager.getRules("rule");
5555
assertEquals(((Map)regexCacheRules.get(ruleManager)).size(), 1);
5656
}
5757

0 commit comments

Comments
 (0)