Skip to content

Commit

Permalink
update keep-alive config
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeCqupt committed Sep 14, 2024
1 parent 783e01d commit 658392c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public DefaultConnectionFactory(Protocol protocol, List<Supplier<ChannelHandler>
this.connectionConfig = connectionConfig;

bootstrap = new Bootstrap();
bootstrap.group(workerGroup).channel(channelClass).handler(new ChannelInitializer<SocketChannel>() {
bootstrap.option(ChannelOption.SO_KEEPALIVE, true)
.group(workerGroup).channel(channelClass).handler(new ChannelInitializer<SocketChannel>() {

@Override
protected void initChannel(SocketChannel ch) throws Exception {
Expand All @@ -94,6 +95,7 @@ protected void initChannel(SocketChannel ch) throws Exception {
public Connection create(SocketAddress socketAddress) throws RemotingException {
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionConfig.getConnectTimeout());
ChannelFuture future = bootstrap.connect(socketAddress);

future.awaitUninterruptibly();
if (!future.isDone()) {
String errMsg = "Create connection to " + socketAddress + " timeout!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void triggerHeartBeat(ChannelHandlerContext ctx) {
int heartbeatFailCount = connection.getHeartbeatFailCnt();
if (heartbeatFailCount > maxFailCount) {
connection.close();
log.error("close connection after heartbeat fail {} times", heartbeatFailCount);
log.error("close connection after heartbeat fail {} times. remote address:{}",
heartbeatFailCount, connection.remoteAddress());
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
Expand Down Expand Up @@ -87,6 +88,7 @@ public void startup() {
this.serverBootstrap = new ServerBootstrap();
this.serverBootstrap.group(bossGroup, workerGroup)
.channel(serverChannelClass)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel channel) throws Exception {
Expand All @@ -97,7 +99,9 @@ protected void initChannel(SocketChannel channel) throws Exception {

if (config.isIdleSwitch()) {
pipeline.addLast("idleStateHandler",
new IdleStateHandler(60000, 60000, 0, TimeUnit.MILLISECONDS));
new IdleStateHandler(config.getIdleReaderTimeout(),
config.getIdleWriterTimeout(),
config.getIdleAllTimeout(), TimeUnit.MILLISECONDS));
pipeline.addLast("serverIdleHandler", serverIdleHandler);
}
pipeline.addLast("handler", handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public class RemotingServerConfig {

private boolean manageConnection = false;

private boolean idleSwitch = false;
private boolean idleSwitch = true;

private long idleReaderTimeout = 0L;

private long idleWriterTimeout = 0L;

private long idleAllTimeout = 90000L;

}

0 comments on commit 658392c

Please sign in to comment.