本文介绍了为什么Netty 4“代理”例子必须设置频道“AUTO_READ”。假的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Netty 4中,频道自动阅读选项已被禁用:

In Netty 4 "proxy" example, the channel auto read option has been disabled:

serverBootStrap.group(bossGroup, workerGroup)
    ...
    .childOption(ChannelOption.AUTO_READ, false)

如果评论过 childOption(ChannelOption.AUTO_READ,false),代理示例将无法正常工作。更详细的,在类 HexDumpProxyFrontendHandler 的方法 channelRead 中, outboundChannel 总是处于非活动状态。

If commented childOption(ChannelOption.AUTO_READ, false), the proxy example will cannot work. And more detailed, in the method channelRead of the class HexDumpProxyFrontendHandler, the outboundChannel will always be inactive.

我研究过Netty源代码,发现自动读取会影响方法类的和 fireChannelReadComplete DefaultChannelPipeline

And I have researched Netty source code, found that "auto read" will affect like that in methods fireChannelActive and fireChannelReadComplete of the class DefaultChannelPipeline

if (channel.config().isAutoRead()) {
    read();
}

但我仍然无法弄清楚自动读取与代理示例之间的关系。在我看来,当数据发送到入站缓冲区时,Netty应该触发频道读取事件。

But I still cannot figure out what the relationship between auto read and the proxy example. In my mind, when a data send to the inbound buffer, Netty should fire the channel read event.

所以有两个问题:


  • 为什么禁用代理示例需要自动读取?

  • 为什么启用自动读取会使 outboundChannel 总是无效?

  • Why disable auto read is necessary for "proxy" example?
  • Why enable auto read will make outboundChannel always inactive?

推荐答案

如果你没有将autoread设置为false如果一个通道在另一个通道消耗之前写入大量数据,则可能会遇到麻烦。因为它都是异步的,所以你可能会将缓冲区放到很多数据上并点击OOME。

if you not set autoread to false you may get into trouble if one channel writes a lot of data before the other can consume it. As it's all asynchronous you may end up buffers to much data and hit OOME.

这篇关于为什么Netty 4“代理”例子必须设置频道“AUTO_READ”。假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:58