Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ Thumbs.db

# Private individual user cursor rules
.cursor/rules/_*.mdc

# local worktrees
.worktrees/
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@ public Mono<Void> execute(final ServerWebExchange exchange, final ShenyuPluginCh
return handleSelectorIfNull(pluginName, exchange, chain);
}
SelectorData selectorData = obtainSelectorDataCacheIfEnabled(path);
// handle Selector
if (Objects.nonNull(selectorData) && StringUtils.isBlank(selectorData.getId())) {
return handleSelectorIfNull(pluginName, exchange, chain);
}
selectorData = defaultMatchSelector(exchange, selectors, path);
if (Objects.isNull(selectorData)) {
return handleSelectorIfNull(pluginName, exchange, chain);
if (Objects.nonNull(selectorData)) {
if (StringUtils.isBlank(selectorData.getId())) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge the condition

return handleSelectorIfNull(pluginName, exchange, chain);
}
} else {
selectorData = defaultMatchSelector(exchange, selectors, path);
if (Objects.isNull(selectorData)) {
return handleSelectorIfNull(pluginName, exchange, chain);
}
}
printLog(selectorData, pluginName);
if (!selectorData.getContinued()) {
Expand All @@ -122,12 +124,15 @@ public Mono<Void> execute(final ServerWebExchange exchange, final ShenyuPluginCh
// if the L1 cache fails to hit, using L2 cache based on trie cache.
// if the L2 cache fails to hit, execute default strategy.
RuleData ruleData = obtainRuleDataCacheIfEnabled(path);
if (Objects.nonNull(ruleData) && Objects.isNull(ruleData.getId())) {
return handleRuleIfNull(pluginName, exchange, chain);
}
ruleData = defaultMatchRule(exchange, rules, path);
if (Objects.isNull(ruleData)) {
return handleRuleIfNull(pluginName, exchange, chain);
if (Objects.nonNull(ruleData)) {
if (Objects.isNull(ruleData.getId())) {
return handleRuleIfNull(pluginName, exchange, chain);
}
} else {
ruleData = defaultMatchRule(exchange, rules, path);
if (Objects.isNull(ruleData)) {
return handleRuleIfNull(pluginName, exchange, chain);
}
}
printLog(ruleData, pluginName);
return doExecute(exchange, chain, selectorData, ruleData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
import org.apache.shenyu.plugin.base.cache.BaseDataCache;
import org.apache.shenyu.plugin.base.cache.MatchDataCache;
import org.apache.shenyu.plugin.base.condition.strategy.MatchStrategyFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
Expand All @@ -42,7 +45,9 @@
import java.util.Collections;
import java.util.List;

import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -248,6 +253,52 @@ public void executeRuleFullTest() {
verify(testShenyuPlugin).doExecute(exchange, shenyuPluginChain, selectorData, ruleData);
}

@Test
public void executeSelectorCacheHitShouldNotReMatchTest() {
List<ConditionData> conditionDataList = Collections.singletonList(conditionData);
this.selectorData.setMatchMode(0);
this.selectorData.setMatchRestful(false);
this.selectorData.setLogged(true);
this.selectorData.setConditionList(conditionDataList);
this.selectorData.setContinued(false);
BaseDataCache.getInstance().cachePluginData(pluginData);
BaseDataCache.getInstance().cacheSelectData(selectorData);
MatchDataCache.getInstance().cacheSelectorData(exchange.getRequest().getURI().getRawPath(), selectorData, 1000, 10000L);
try (MockedStatic<MatchStrategyFactory> mockedStatic = Mockito.mockStatic(MatchStrategyFactory.class)) {
mockedStatic.when(() -> MatchStrategyFactory.match(selectorData.getMatchMode(), selectorData.getConditionList(), exchange)).thenReturn(true);
StepVerifier.create(testShenyuPlugin.execute(exchange, shenyuPluginChain)).expectSubscription().verifyComplete();
verify(testShenyuPlugin).doExecute(Mockito.same(exchange), Mockito.same(shenyuPluginChain), Mockito.same(selectorData),
argThat(rule -> Constants.DEFAULT_RULE.equals(rule.getId()) && selectorData.getId().equals(rule.getSelectorId())));
mockedStatic.verifyNoInteractions();
verify(shenyuPluginChain, never()).execute(exchange);
}
}

@Test
public void executeRuleCacheHitShouldNotReMatchTest() {
List<ConditionData> conditionDataList = Collections.singletonList(conditionData);
this.selectorData.setMatchMode(0);
this.selectorData.setMatchRestful(false);
this.selectorData.setLogged(true);
this.selectorData.setConditionList(conditionDataList);
this.selectorData.setContinued(true);
this.ruleData.setConditionDataList(conditionDataList);
this.ruleData.setMatchMode(0);
this.ruleData.setMatchRestful(false);
BaseDataCache.getInstance().cachePluginData(pluginData);
BaseDataCache.getInstance().cacheSelectData(selectorData);
BaseDataCache.getInstance().cacheRuleData(ruleData);
MatchDataCache.getInstance().cacheSelectorData(exchange.getRequest().getURI().getRawPath(), selectorData, 1000, 10000L);
MatchDataCache.getInstance().cacheRuleData(exchange.getRequest().getURI().getRawPath(), ruleData, 1000, 10000L);
try (MockedStatic<MatchStrategyFactory> mockedStatic = Mockito.mockStatic(MatchStrategyFactory.class)) {
mockedStatic.when(() -> MatchStrategyFactory.match(selectorData.getMatchMode(), selectorData.getConditionList(), exchange)).thenReturn(true);
mockedStatic.when(() -> MatchStrategyFactory.match(ruleData.getMatchMode(), ruleData.getConditionDataList(), exchange)).thenReturn(true);
StepVerifier.create(testShenyuPlugin.execute(exchange, shenyuPluginChain)).expectSubscription().verifyComplete();
verify(testShenyuPlugin).doExecute(exchange, shenyuPluginChain, selectorData, ruleData);
mockedStatic.verifyNoInteractions();
}
}

@AfterEach
public void clear() {
MatchDataCache.getInstance().cleanSelectorData();
Expand Down
Loading