Skip to content

Commit

Permalink
Add router fail fast option (Backport #9388) (#11188)
Browse files Browse the repository at this point in the history
* Add router fail fast option (Backport #9388)

* update
  • Loading branch information
AlbumenJ authored Dec 21, 2022
1 parent 326fb72 commit bf29eb5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ public interface Constants {
* prefix of arguments router key
*/
String ARGUMENTS = "arguments";

String SHOULD_FAIL_FAST_KEY = "dubbo.router.should-fail-fast";
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.common.Version;
import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
Expand All @@ -33,6 +34,7 @@
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.Cluster;
import org.apache.dubbo.rpc.cluster.Configurator;
import org.apache.dubbo.rpc.cluster.Constants;
import org.apache.dubbo.rpc.cluster.RouterChain;
import org.apache.dubbo.rpc.cluster.RouterFactory;
import org.apache.dubbo.rpc.cluster.directory.AbstractDirectory;
Expand Down Expand Up @@ -94,6 +96,11 @@ public abstract class DynamicDirectory<T> extends AbstractDirectory<T> implement

protected ServiceInstancesChangedListener serviceListener;

/**
* Should continue route if directory is empty
*/
private final boolean shouldFailFast;

public DynamicDirectory(Class<T> serviceType, URL url) {
super(url, true);

Expand All @@ -114,6 +121,7 @@ public DynamicDirectory(Class<T> serviceType, URL url) {
this.overrideDirectoryUrl = this.directoryUrl = turnRegistryUrlToConsumerUrl(url);
String group = directoryUrl.getParameter(GROUP_KEY, "");
this.multiGroup = group != null && (ANY_VALUE.equals(group) || group.contains(","));
this.shouldFailFast = Boolean.parseBoolean(ConfigurationUtils.getProperty(Constants.SHOULD_FAIL_FAST_KEY, "true"));
}

@Override
Expand Down Expand Up @@ -162,7 +170,7 @@ public void unSubscribe(URL url) {

@Override
public List<Invoker<T>> doList(Invocation invocation) {
if (forbidden) {
if (forbidden && shouldFailFast) {
// 1. No service provider 2. Service providers are disabled
throw new RpcException(RpcException.FORBIDDEN_EXCEPTION, "No provider available from registry " +
getUrl().getAddress() + " for service " + getConsumerUrl().getServiceKey() + " on consumer " +
Expand Down

0 comments on commit bf29eb5

Please sign in to comment.