Skip to content

Commit

Permalink
[ISSUE #7056] Avoid close success channel if invokeSync most time cos…
Browse files Browse the repository at this point in the history
…t on get connection for channel (#7057)

* fix: avoid close success channel if invokeSync most time cost on get channel

Change-Id: I29741cf55ac6333bfa30fef755357b78a22b1325

* fix: ci style

Change-Id: I8c9b86e9cb6f1463bf213e64c9b8c139afa794c8
  • Loading branch information
absolute8511 committed Jul 27, 2023
1 parent 32eb1d5 commit d797377
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class NettyRemotingClient extends NettyRemotingAbstract implements Remoti
private static final Logger LOGGER = LoggerFactory.getLogger(LoggerName.ROCKETMQ_REMOTING_NAME);

private static final long LOCK_TIMEOUT_MILLIS = 3000;
private static final long MIN_CLOSE_TIMEOUT_MILLIS = 100;

private final NettyClientConfig nettyClientConfig;
private final Bootstrap bootstrap = new Bootstrap();
Expand Down Expand Up @@ -524,13 +525,15 @@ public RemotingCommand invokeSync(String addr, final RemotingCommand request, lo
final Channel channel = this.getAndCreateChannel(addr);
String channelRemoteAddr = RemotingHelper.parseChannelRemoteAddr(channel);
if (channel != null && channel.isActive()) {
long left = timeoutMillis;
try {
doBeforeRpcHooks(channelRemoteAddr, request);
long costTime = System.currentTimeMillis() - beginStartTime;
if (timeoutMillis < costTime) {
left -= costTime;
if (left <= 0) {
throw new RemotingTimeoutException("invokeSync call the addr[" + channelRemoteAddr + "] timeout");
}
RemotingCommand response = this.invokeSyncImpl(channel, request, timeoutMillis - costTime);
RemotingCommand response = this.invokeSyncImpl(channel, request, left);
doAfterRpcHooks(channelRemoteAddr, request, response);
this.updateChannelLastResponseTime(addr);
return response;
Expand All @@ -539,7 +542,9 @@ public RemotingCommand invokeSync(String addr, final RemotingCommand request, lo
this.closeChannel(addr, channel);
throw e;
} catch (RemotingTimeoutException e) {
if (nettyClientConfig.isClientCloseSocketIfTimeout()) {
// avoid close the success channel if left timeout is small, since it may cost too much time in get the success channel, the left timeout for read is small
boolean shouldClose = left > MIN_CLOSE_TIMEOUT_MILLIS || left > timeoutMillis / 4;
if (nettyClientConfig.isClientCloseSocketIfTimeout() && shouldClose) {
this.closeChannel(addr, channel);
LOGGER.warn("invokeSync: close socket because of timeout, {}ms, {}", timeoutMillis, channelRemoteAddr);
}
Expand Down

0 comments on commit d797377

Please sign in to comment.