Skip to content

Commit

Permalink
优化TCP相关参数
Browse files Browse the repository at this point in the history
  • Loading branch information
CC-Cheunggg committed May 1, 2020
1 parent ae7a9bf commit eec9a1c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/cheung/shadowsocks/common/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public void init() {
.channel(EpollSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
.option(ChannelOption.SO_KEEPALIVE,true)
.option(ChannelOption.SO_TIMEOUT, 120 * 1000)
.option(ChannelOption.SO_SNDBUF, 128 * 5 * 120 * 1024)
.option(ChannelOption.SO_RCVBUF, 128 * 5 * 120 * 1024)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.AUTO_READ, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {

Channel channel = getClientProxyChannel(ctx).channel();
ChannelHandlerContext context = getClientProxyChannel(ctx);

// 等待 发送缓冲区 的 数据发送完 为减少 Connection reset by peer
while ((channel != null) && (channel.isActive()) && (!(channel.unsafe().outboundBuffer().isEmpty()))) {
Thread.sleep(2000);
}
// while ((channel != null) && (channel.isActive()) && (!(channel.unsafe().outboundBuffer().isEmpty()))) {
// Thread.sleep(2000);
// }

ctx.close();
getClientProxyChannel(ctx).close();
context.close();
logger.info("InternetDataHandler channelInactive close Interview address = {}", ctx.channel().remoteAddress());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public void start() {
.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
.childOption(ChannelOption.AUTO_READ, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.SO_LINGER, 2)
.childOption(ChannelOption.SO_BACKLOG, 1024)
.childOption(ChannelOption.SO_TIMEOUT, 120 * 1000)
.childOption(ChannelOption.SO_SNDBUF, 128 * 5 * 120 * 1024)
.childOption(ChannelOption.SO_RCVBUF, 128 * 5 * 120 * 1024)
.childOption(ChannelOption.TCP_NODELAY, true);

bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
Expand Down

0 comments on commit eec9a1c

Please sign in to comment.