From f1032615478bb8779296567c5dc9b823657a5145 Mon Sep 17 00:00:00 2001 From: "liming.1018" Date: Tue, 20 Feb 2024 17:09:44 +0800 Subject: [PATCH] fix the issue in RpcChannel that callback was not notified when writeAndFlush failed. --- .../com/baidu/jprotobuf/pbrpc/transport/RpcChannel.java | 8 +++++++- .../jprotobuf/pbrpc/transport/handler/ErrorCodes.java | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/RpcChannel.java b/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/RpcChannel.java index 7a35144f..8a65f1b3 100644 --- a/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/RpcChannel.java +++ b/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/RpcChannel.java @@ -22,8 +22,10 @@ import org.slf4j.LoggerFactory; import com.baidu.jprotobuf.pbrpc.data.RpcDataPackage; +import com.baidu.jprotobuf.pbrpc.transport.handler.ErrorCodes; import io.netty.channel.Channel; +import io.netty.channel.ChannelFutureListener; import io.netty.util.Timeout; /** @@ -138,7 +140,11 @@ public void doTransport(Connection connection, RpcDataPackage rpcDataPackage, LOG.debug("Do send request with service name '" + rpcDataPackage.serviceName() + "' method name '" + rpcDataPackage.methodName() + "' bound channel =>" + channel); - channel.writeAndFlush(state.getDataPackage()); + channel.writeAndFlush(state.getDataPackage()).addListener((ChannelFutureListener) channelFuture -> { + if (!channelFuture.isSuccess() && channelFuture.cause() != null) { + state.handleFailure(ErrorCodes.ST_WRITE_FAILED, channelFuture.cause().getMessage()); + } + }); } long callMethodEnd = System.currentTimeMillis(); diff --git a/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/handler/ErrorCodes.java b/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/handler/ErrorCodes.java index 60ee602b..fed3318c 100644 --- a/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/handler/ErrorCodes.java +++ b/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/handler/ErrorCodes.java @@ -36,6 +36,9 @@ public class ErrorCodes { /** read time out. */ public static final int ST_READ_TIMEOUT = 62; + /** 消息发送异常. */ + public static final int ST_WRITE_FAILED = 4001; + /** onceTalkTimeout timeout message. */ public static final String MSG_READ_TIMEOUT = "method request time out, please check 'onceTalkTimeout' property. current value is:";