What happens to writes when ChannelOutboundBuffer is full

独自空忆成欢 提交于 2020-12-09 09:56:57

问题


I'm trying to figure out what happens to write requests when a channel is non-responsive. For example, this happens when the peer suddenly drops off the network, and we never get a RST.

From reading docs and Understanding netty channel buffers and watermarks, it seems that once the high WriterBufferWaterMark is reached, ChannelOutboundBuffer will be considered full, and:

Channel.isWritable() will start to return false.

From Channel.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.

My initial question was: What happens when we keep writing anyway?

Where will the data been queued if Netty channel isWritable return false states that there is an internal buffer, and implies it is unbounded.

The question becomes, where exactly are the write requests queued, and more importantly can I observe the size of these queues?

Or: is there some limit when Netty/OS will detect that the connection is broken and needs to be closed?


回答1:


It will still be queued in ChannelOutboundBuffer. You are responsible to stop writing once the buffer is full and start again one it empties. You can observe this via ChannelInboundHandler.channelWritabilityChanged(...).




回答2:


Looking at the source for ChannelOutboundBuffer in 4.1.37, the method addMessage always adds incoming messages to an internal linked list, regardless of the state of isWritable. This list grows unbounded.

The total byte length of this buffer is reachable via channel.unsafe().outboundBuffer().totalPendingWriteBytes().

For an indirect value, if you don't want to use unsafe(), you can alternatively use channel.bytesBeforeWritable() which will return the queued bytes minus the low watermark.



来源:https://stackoverflow.com/questions/64930528/what-happens-to-writes-when-channeloutboundbuffer-is-full

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!