Skip to content

Commit

Permalink
fix(networking-timeout): graceful shutdown after failed connection (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkWeird authored Nov 30, 2020
1 parent 99feea2 commit 1f7ea2a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ClientConnectionHandler extends ChannelInboundHandlerAdapter {
private Path tempModuleLocation;
private BufferedOutputStream downloadingModule;
private long lengthReceived;
private Timer timeoutTimer = new Timer();
private Timer timeoutTimer = new Timer("Netty-Timeout-Timer", true);
private long timeoutPoint = System.currentTimeMillis();
private final long timeoutThreshold = 120000;
private Channel channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public class NetworkSystemImpl implements EntityChangeSubscriber, NetworkSystem

// Client only
private ServerImpl server;
private EventLoopGroup clientGroup;

public NetworkSystemImpl(Time time, Context context) {
this.time = time;
Expand Down Expand Up @@ -219,11 +220,11 @@ public JoinStatus join(String address, int port) throws InterruptedException {
}
ChannelFuture connectCheck = null;

EventLoopGroup group = new NioEventLoopGroup();
clientGroup = new NioEventLoopGroup();
try {
Bootstrap clientBootstrap = new Bootstrap();

clientBootstrap.group(group);
clientBootstrap.group(clientGroup);
clientBootstrap.channel(NioSocketChannel.class);
clientBootstrap.option(ChannelOption.SO_KEEPALIVE, true);
clientBootstrap.option(ChannelOption.TCP_NODELAY, true);
Expand All @@ -247,13 +248,17 @@ public JoinStatus join(String address, int port) throws InterruptedException {
} else {
logger.warn("Failed to connect to server", connectCheck.cause());
connectCheck.channel().closeFuture().awaitUninterruptibly();
clientGroup.shutdownGracefully().syncUninterruptibly();
return new JoinStatusImpl("Failed to connect to server - " + connectCheck.cause().getMessage());
}
}
connectCheck.channel().closeFuture().sync();
} catch (InterruptedException e) {
connectCheck.cancel(true);
connectCheck.channel().closeFuture().awaitUninterruptibly();
} catch (Exception e) {
shutdown();
if (connectCheck != null) {
connectCheck.cancel(true);
connectCheck.channel().closeFuture().awaitUninterruptibly();
}
throw e;
}
}
Expand All @@ -265,11 +270,10 @@ public void shutdown() {
allChannels.close().awaitUninterruptibly();
if (serverChannelFuture != null) {
serverChannelFuture.channel().closeFuture();
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();

// Wait until all threads are terminated.
try {
bossGroup.shutdownGracefully().sync();
workerGroup.shutdownGracefully().sync();
bossGroup.terminationFuture().sync();
workerGroup.terminationFuture().sync();
} catch (InterruptedException e) {
Expand All @@ -278,6 +282,9 @@ public void shutdown() {
}

}
if (clientGroup != null) {
clientGroup.shutdownGracefully().syncUninterruptibly();
}
// Shut down all event loops to terminate all threads.

processPendingDisconnects();
Expand Down

0 comments on commit 1f7ea2a

Please sign in to comment.