我在一个使用Netty的套接字服务器的开发团队。当客户端发送请求,服务器发送单个响应时,往返时间相当快。(好)我们最近注意到,如果来自客户端的请求触发了来自服务器的两条消息,即使服务器几乎同时向客户端写入两条消息,到达远程客户端的第一条消息和第二条消息之间也会有超过200ms的延迟。当使用本地客户机时,两条消息同时到达。如果远程客户端在来自服务器的第二条消息到达之前发送另一个请求,则立即发送第二条消息,但随后新请求的两条消息都以超过200ms的延迟发送。
因为在使用Netty 3.3.1时注意到,我尝试升级到Netty 3.6.5,但我仍然看到相同的行为。我们使用NIO,而不是OIO,因为我们需要能够支持大量并发客户端。
是否有一个设置,我们需要配置,将减少200+ ms的延迟?
编辑以添加代码片段。我希望这些是最相关的部分。
@Override
public boolean openListener(final Protocol protocol,
InetSocketAddress inetSocketAddress) throws Exception {
ChannelFactory factory = new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool(),
threadingConfiguration.getProcessorThreadCount());
ServerBootstrap bootstrap = new ServerBootstrap(factory);
final ChannelGroup channelGroup = new DefaultChannelGroup();
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
.... lots of pipeline setup snipped ......
});
Channel channel = bootstrap.bind(inetSocketAddress);
channelGroup.add(channel);
channelGroups.add(channelGroup);
bootstraps.add(bootstrap);
return true;
}
写入器工厂使用ChannelBuffers.dynamicBuffer(defaultMessageSize)作为缓冲区,当我们写入消息时,它是Channels。写(通道、味精)。
还有什么有用的?将代码迁移到Netty的开发人员目前不可用,我正在尝试填充。
200ms对我来说是内格尔算法的神奇数字。尝试将两端的TcpNoDelay设置为true。
这是为服务器端设置选项的方法。
serverBootstrap.setOption("child.tcpNoDelay", true);
这是客户端。
clientBootStrap.setOption("tcpNoDelay", true);
进一步阅读:http://www.stuartcheshire.org/papers/NagleDelayedAck/