1

From the Netty api, it shows the request will be queued if isWritable return false. Could I know where will the request be queued? In what case, the queue could be full and cause OOM issue?

Following is the document for isWritable()

Returns true if and only if the I/O thread will perform the requested write operation immediately. Any write requests made when this method returns false are queued until the I/O thread is ready to process the queued write requests.

https://netty.io/4.1/api/io/netty/channel/Channel.html#isWritable--

wguo
  • 55
  • 1
  • 6

1 Answers1

2

It will be queued in an internal buffer maintained by netty. To avoid the system from going OOM you will have to override the method channelWritabilityChanged of the ChannelInboundHandler and do back pressure handling.

Here you can slow down the reading of the inbound data by using the autoread config of the channel and manually read the request as described here. Or if you are writing on a different thread you might need to block that thread.

Riyafa Abdul Hameed
  • 7,417
  • 6
  • 40
  • 55
  • Thanks Riyafa. Since netty queues the request into the internal buffer. If my client keep sending data, It will first out of the memory of the java heap memory? We use ChannelOutboundHandler to send the data. Do you know why the channelWritabilityChanged is only in ChannelInboundHandler? – wguo Dec 18 '18 at 17:54
  • You would be initiating write outside the `ChannelOutboundHandler`. There is not much back pressure handling you can do in the `ChannelOutboundHandler` as it is in the netty thread. – Riyafa Abdul Hameed Dec 19 '18 at 03:23
  • The messages are buffered in [ChannelOutboundBuffer](https://github.com/netty/netty/blob/4.1/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java). OOM may happen if you keep writing data while the channel is not writable, because netty basically keeps buffering the data you ask to write in memory (in that ChannelOutboundBuffer). Check these slides as well http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#9.0 – Artashes Aghajanyan Dec 21 '18 at 20:57
  • If you want to know _exactly_ where the data goes, I found mroe info here: https://stackoverflow.com/a/65004753/1207791 – cfstras Nov 25 '20 at 12:32