Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support intercept specified command handler #2587

Merged
merged 4 commits into from
Mar 29, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add unit test and demo
icodening committed Mar 23, 2022
commit 192d158fcfcc7fd01fb445b7c94d73eef4c7c3a0
4 changes: 4 additions & 0 deletions sentinel-demo/sentinel-demo-command-handler/pom.xml
Original file line number Diff line number Diff line change
@@ -16,5 +16,9 @@
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
<dependency>
icodening marked this conversation as resolved.
Show resolved Hide resolved
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-common</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.demo.commandhandler.interceptor;

import com.alibaba.csp.sentinel.command.CommandHandlerInterceptor;
import com.alibaba.csp.sentinel.command.CommandRequest;
import com.alibaba.csp.sentinel.command.CommandRequestExecution;
import com.alibaba.csp.sentinel.command.CommandResponse;
import com.alibaba.csp.sentinel.spi.Spi;

/**
* @author icodening
* @date 2022.03.23
*/
@Spi(order = Spi.ORDER_HIGHEST)
public class AllCommandHandlerInterceptor implements CommandHandlerInterceptor {

@Override
public boolean shouldIntercept(String commandName) {
return true;
}

@Override
public CommandResponse intercept(CommandRequest request, CommandRequestExecution execution) {
System.out.println("[AllCommandHandlerInterceptor] start");
long begin = System.currentTimeMillis();
try {
return execution.execute(request);
} catch (Throwable throwable) {
System.out.println("[AllCommandHandlerInterceptor] catch exception: " + throwable.getMessage());
throw throwable;
} finally {
long cost = System.currentTimeMillis() - begin;
System.out.println("[AllCommandHandlerInterceptor] complete, cost " + cost + "ms");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.demo.commandhandler.interceptor;

import com.alibaba.csp.sentinel.command.CommandHandlerInterceptor;
import com.alibaba.csp.sentinel.command.CommandRequest;
import com.alibaba.csp.sentinel.command.CommandRequestExecution;
import com.alibaba.csp.sentinel.command.CommandResponse;

/**
* @author icodening
* @date 2022.03.23
*/
public class EchoCommandHandlerInterceptor implements CommandHandlerInterceptor<String> {

@Override
public boolean shouldIntercept(String commandName) {
return "echo".equals(commandName);
}

@Override
public CommandResponse<String> intercept(CommandRequest request, CommandRequestExecution<String> execution) {
System.out.println("[EchoCommandHandlerInterceptor] intercept 'echo' command,all params is " + request.getParameters() + "");
CommandResponse<String> response = execution.execute(request);
return CommandResponse.ofSuccess("intercept result : [" + response.getResult() + "]");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
com.alibaba.csp.sentinel.demo.commandhandler.interceptor.EchoCommandHandlerInterceptor
com.alibaba.csp.sentinel.demo.commandhandler.interceptor.AllCommandHandlerInterceptor
Original file line number Diff line number Diff line change
@@ -15,13 +15,13 @@
*/
package com.alibaba.csp.sentinel.command;

import java.util.*;

import com.alibaba.csp.sentinel.command.annotation.CommandMapping;
import com.alibaba.csp.sentinel.command.handler.InterceptingCommandHandler;
import com.alibaba.csp.sentinel.spi.SpiLoader;
import com.alibaba.csp.sentinel.util.StringUtil;

import java.util.*;

/**
* Provides and filters command handlers registered via SPI.
*
@@ -42,18 +42,21 @@ public Map<String, CommandHandler> namedHandlers() {
List<CommandHandlerInterceptor> commandHandlerInterceptors = SpiLoader.of(CommandHandlerInterceptor.class).loadInstanceListSorted();
for (CommandHandler handler : handlers) {
String name = parseCommandName(handler);
if (StringUtil.isEmpty(name)) {
continue;
}
if (!commandHandlerInterceptors.isEmpty()) {
List<CommandHandlerInterceptor> interceptors = new ArrayList<>();
for (CommandHandlerInterceptor commandHandlerInterceptor : commandHandlerInterceptors) {
if (commandHandlerInterceptor.shouldIntercept(name)) {
interceptors.add(commandHandlerInterceptor);
}
}
handler = new InterceptingCommandHandler(handler, interceptors);
}
if (!StringUtil.isEmpty(name)) {
map.put(name, handler);
if (!interceptors.isEmpty()) {
handler = new InterceptingCommandHandler(handler, interceptors);
}
}
map.put(name, handler);
}
return map;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.transport.command;

import com.alibaba.csp.sentinel.command.CommandHandlerInterceptor;
import com.alibaba.csp.sentinel.command.CommandRequest;
import com.alibaba.csp.sentinel.command.CommandRequestExecution;
import com.alibaba.csp.sentinel.command.CommandResponse;

/**
* @author icodening
* @date 2022.03.23
*/
public class GetRulesCommandHandlerInterceptor implements CommandHandlerInterceptor {

@Override
public boolean shouldIntercept(String commandName) {
return "getRules".equals(commandName);
}

@Override
public CommandResponse intercept(CommandRequest request, CommandRequestExecution execution) {
String type = request.getParam("type");
System.out.println("[GetRulesCommandHandlerInterceptor] get rules for [" + type + "]");
return execution.execute(request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.transport.command;

import com.alibaba.csp.sentinel.command.CommandHandler;
import com.alibaba.csp.sentinel.command.CommandHandlerProvider;
import com.alibaba.csp.sentinel.command.CommandRequest;
import com.alibaba.csp.sentinel.command.CommandResponse;
import com.alibaba.csp.sentinel.command.handler.InterceptingCommandHandler;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.transport.util.HttpCommandUtils;
import com.alibaba.csp.sentinel.util.AssertUtil;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;
import java.util.Map;

/**
* @author icodening
* @date 2022.03.23
*/
@SuppressWarnings("all")
public class InterceptingCommandHandlerTest {

private Map<String, CommandHandler> commandHandlerMap = CommandHandlerProvider.getInstance().namedHandlers();

@Before
public void setUp() throws Exception {
FlowRule flowRule = new FlowRule();
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS)
.setClusterMode(false)
.setCount(1)
.setResource("/test");
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
}

@Test
public void testInterceptCommand() {
CommandHandler getRulesHandler = commandHandlerMap.get("getRules");
AssertUtil.assertState(getRulesHandler instanceof InterceptingCommandHandler, "getRulesHandler should be an InterceptingCommandHandler");

CommandHandler basicInfoHandler = commandHandlerMap.get("basicInfo");
AssertUtil.assertState(!(basicInfoHandler instanceof InterceptingCommandHandler), "basicInfoHandler should not be an InterceptingCommandHandler");

CommandRequest getRulesCommandRequest = new CommandRequest();
getRulesCommandRequest.addMetadata(HttpCommandUtils.REQUEST_TARGET, "getRules");
getRulesCommandRequest.addParam("type", "flow");
CommandResponse getRulesResponse = getRulesHandler.handle(getRulesCommandRequest);
System.out.println(getRulesResponse.getResult());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.alibaba.csp.sentinel.transport.command.GetRulesCommandHandlerInterceptor