【fix】 自定义netty 接收数据缓冲期控制接收数据大小

This commit is contained in:
wujiawei
2024-08-30 17:12:23 +08:00
parent 629910e860
commit 58aae7a67d
13 changed files with 132 additions and 79 deletions

View File

@ -4,6 +4,8 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.compression.JdkZlibDecoder;
import io.netty.handler.codec.compression.JdkZlibEncoder;
import org.framework.lazy.cloud.network.heartbeat.client.netty.handler.NettyClientRealHandler;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.TransferEncoder;
@ -22,5 +24,6 @@ public class NettyClientRealFilter extends ChannelInitializer<SocketChannel> {
pipeline.addLast(new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024*10));
pipeline.addLast(new TransferEncoder());
pipeline.addLast(new NettyClientRealHandler());
}
}

View File

@ -58,9 +58,9 @@ public class NettyClientRealSocket {
// .option(ChannelOption.SO_RCVBUF, 2048 * 1024)
// 设置写缓冲区为1M
// .option(ChannelOption.SO_SNDBUF, 1024 * 1024)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
.option(ChannelOption.SO_BACKLOG, 128)//务端接受连接的队列长度 默认128
// .option(ChannelOption.SO_BACKLOG, 128)//务端接受连接的队列长度 默认128
.option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.handler(new NettyClientRealFilter())
@ -136,6 +136,14 @@ public class NettyClientRealSocket {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
.option(ChannelOption.SO_BACKLOG, 256)//务端接受连接的队列长度 默认128
.option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, WriteBufferWaterMark.DEFAULT)
.handler(new NettyClientVisitorRealFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList)))
;