Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Netty 4 "proxy" example has to set channel "AUTO_READ" as false

Tags:

java

netty

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

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

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.

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();
}

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.

So there are two questions:

  • Why disable auto read is necessary for "proxy" example?
  • Why enable auto read will make outboundChannel always inactive?
like image 953
Yang Lifan Avatar asked Feb 02 '15 08:02

Yang Lifan


1 Answers

If you have 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 with buffers that have too much data and hit OOME.

like image 105
Norman Maurer Avatar answered Nov 20 '22 02:11

Norman Maurer