注意:我们分析的sofa-rpc版本是5.4.0。

图1 FailFastCluster的类继承图
Failfast可以理解为只发起一次调用,若失败则立即报错。
2.sofa-rpc中Failfast的实现
核心代码在FailFastCluster的doInvoke(SofaRequest request)中,源码如下。
@Override
public SofaResponse doInvoke(SofaRequest request) throws SofaRpcException {
ProviderInfo providerInfo = select(request);
try {
SofaResponse response = filterChain(providerInfo, request);
if (response != null) {
return response;
} else {
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR,
"Failed to call " + request.getInterfaceName() + "." + request.getMethodName()
+ " on remote server " + providerInfo + ", return null");
}
} catch (Exception e) {
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR,
"Failed to call " + request.getInterfaceName() + "." + request.getMethodName()
+ " on remote server: " + providerInfo + ", cause by: "
+ e.getClass().getName() + ", message is: " + e.getMessage(), e);
}
}重点在于请求异常之后,立即将异常抛出给调用者。
你是否在其它框架中看到过Failfast的实现? Dubbo中有Failfast的实现,可以去看Dubbo中FailfastClusterInvoker.java的实现,我的博客中分析过。
(adsbygoogle = window.adsbygoogle || []).push({});