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
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.command;

/**
* intercept specified command, and can be extended using SPI
*
* @author icodening
* @date 2022.03.03
* @see com.alibaba.csp.sentinel.spi.SpiLoader
* @see com.alibaba.csp.sentinel.spi.Spi
*/
public interface CommandHandlerInterceptor<R> {

/**
* whether to intercept the specified command
*
* @param commandName command name, eg. getRules
* @return "true" means intercept, "false" means skip
*/
boolean shouldIntercept(String commandName);

/**
* intercept the given command request, and return a command response
*
* @param request commandRequest
* @param execution interceptor chain execution
* @return command response
*/
CommandResponse<R> intercept(CommandRequest request, CommandRequestExecution<R> execution);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +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.
*
Expand All @@ -38,8 +39,18 @@ public class CommandHandlerProvider implements Iterable<CommandHandler> {
public Map<String, CommandHandler> namedHandlers() {
Map<String, CommandHandler> map = new HashMap<String, CommandHandler>();
List<CommandHandler> handlers = spiLoader.loadInstanceList();
List<CommandHandlerInterceptor> commandHandlerInterceptors = SpiLoader.of(CommandHandlerInterceptor.class).loadInstanceListSorted();
for (CommandHandler handler : handlers) {
String name = parseCommandName(handler);
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.command;

/**
* @author icodening
* @date 2022.03.03
*/
public interface CommandRequestExecution<R> {

/**
* execute the command request and return the command response.
*
* @param request command request
* @return command response
*/
CommandResponse<R> execute(CommandRequest request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.command.handler;

import com.alibaba.csp.sentinel.command.*;
import com.alibaba.csp.sentinel.util.AssertUtil;

import java.util.Iterator;
import java.util.List;
import java.util.function.Function;

/**
* intercept specified command handler
*
* @author icodening
* @date 2022.03.03
*/
public class InterceptingCommandHandler<R> implements CommandHandler<R> {

private final CommandHandler<R> delegate;

private final List<CommandHandlerInterceptor<R>> commandHandlerInterceptors;

public InterceptingCommandHandler(CommandHandler<R> delegate, List<CommandHandlerInterceptor<R>> commandHandlerInterceptors) {
AssertUtil.notNull(delegate, "delegate cannot be null");
AssertUtil.notNull(commandHandlerInterceptors, "commandHandlerInterceptors cannot be null");
this.delegate = delegate;
this.commandHandlerInterceptors = commandHandlerInterceptors;
}

@Override
public CommandResponse<R> handle(CommandRequest request) {
return new InterceptingRequestExecution<>(commandHandlerInterceptors.iterator(), delegate::handle).execute(request);
}

private static class InterceptingRequestExecution<R> implements CommandRequestExecution<R> {

private final Iterator<CommandHandlerInterceptor<R>> iterator;

private final Function<CommandRequest, CommandResponse<R>> commandResponseFunction;

public InterceptingRequestExecution(Iterator<CommandHandlerInterceptor<R>> iterator,
Function<CommandRequest, CommandResponse<R>> commandResponseFunction) {
this.iterator = iterator;
this.commandResponseFunction = commandResponseFunction;
}

@Override
public CommandResponse<R> execute(CommandRequest request) {
if (this.iterator.hasNext()) {
CommandHandlerInterceptor<R> nextInterceptor = this.iterator.next();
return nextInterceptor.intercept(request, this);
}
return commandResponseFunction.apply(request);
}
}
}