Netty 中的套接字接受延迟
Socket accept delay in Netty
有时,在Netty 服务器建立套接字连接之前,客户端必须发送大量SYN 数据包。例如,查看一些使用 tcpdump
制作的数据包
12:23:30.166272 IP (tos 0x0, ttl 53, id 61857, offset 0, flags [DF], proto TCP (6), length 60)
SOURCE.25809 > DEST.7780: Flags [S], cksum 0x4bf4 (correct), seq 3364886260, win 5200, options [mss 1380,nop,wscale 0,nop,nop,TS val 264 ecr 0], length 0
12:23:54.067379 IP (tos 0x0, ttl 53, id 61858, offset 0, flags [DF], proto TCP (6), length 60)
SOURCE.53156 > DEST.7780: Flags [S], cksum 0x2b21 (correct), seq 2559114443, win 5200, options [mss 1380,nop,wscale 0,nop,nop,TS val 312 ecr 0], length 0
12:24:30.027630 IP (tos 0x0, ttl 53, id 61859, offset 0, flags [DF], proto TCP (6), length 60)
SOURCE.40667 > DEST.7780: Flags [S], cksum 0x0feb (correct), seq 3417642326, win 5200, options [mss 1380,nop,wscale 0,nop,nop,TS val 384 ecr 0], length 0
Netty服务器启动相关代码如下:
static final DefaultEventExecutorGroup e1 = new DefaultEventExecutorGroup(3);
static final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
static final EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.childHandler(new SocketChannelInitializer(cpds, e1, redispool))
.childOption(ChannelOption.TCP_NODELAY, false)
.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS,2000)
.childOption(ChannelOption.SO_REUSEADDR, true);
ChannelFuture future = b.bind(address);
future.syncUninterruptibly();
channel = future.channel();
logger.info("Binded server to port: " + System.getProperty("port"));
return future;
希望有人能给我提示。
更新:
我发现这是 linux 内核上的 TCP 问题,net.ipv4.tcp_tw_recycle = 0
解决了我的问题!
我发现这是 linux 内核 net 上的 TCP 问题。ipv4.tcp_tw_recycle = 0 解决了我的问题!
有时,在Netty 服务器建立套接字连接之前,客户端必须发送大量SYN 数据包。例如,查看一些使用 tcpdump
12:23:30.166272 IP (tos 0x0, ttl 53, id 61857, offset 0, flags [DF], proto TCP (6), length 60)
SOURCE.25809 > DEST.7780: Flags [S], cksum 0x4bf4 (correct), seq 3364886260, win 5200, options [mss 1380,nop,wscale 0,nop,nop,TS val 264 ecr 0], length 0
12:23:54.067379 IP (tos 0x0, ttl 53, id 61858, offset 0, flags [DF], proto TCP (6), length 60)
SOURCE.53156 > DEST.7780: Flags [S], cksum 0x2b21 (correct), seq 2559114443, win 5200, options [mss 1380,nop,wscale 0,nop,nop,TS val 312 ecr 0], length 0
12:24:30.027630 IP (tos 0x0, ttl 53, id 61859, offset 0, flags [DF], proto TCP (6), length 60)
SOURCE.40667 > DEST.7780: Flags [S], cksum 0x0feb (correct), seq 3417642326, win 5200, options [mss 1380,nop,wscale 0,nop,nop,TS val 384 ecr 0], length 0
Netty服务器启动相关代码如下:
static final DefaultEventExecutorGroup e1 = new DefaultEventExecutorGroup(3);
static final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
static final EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.childHandler(new SocketChannelInitializer(cpds, e1, redispool))
.childOption(ChannelOption.TCP_NODELAY, false)
.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS,2000)
.childOption(ChannelOption.SO_REUSEADDR, true);
ChannelFuture future = b.bind(address);
future.syncUninterruptibly();
channel = future.channel();
logger.info("Binded server to port: " + System.getProperty("port"));
return future;
希望有人能给我提示。
更新:
我发现这是 linux 内核上的 TCP 问题,net.ipv4.tcp_tw_recycle = 0
解决了我的问题!
我发现这是 linux 内核 net 上的 TCP 问题。ipv4.tcp_tw_recycle = 0 解决了我的问题!