首页 > 其他分享 >Netty4 读写水位控制分析

Netty4 读写水位控制分析

时间:2022-12-27 19:04:34浏览次数:58  
标签:high watermark BUFFER WATER 读写 queue 水位 low Netty4


服务器上看看默认是多少

Configure high and low write watermarks

Set sane WRITE_BUFFER_HIGH_WATER_MARK andWRITE_BUFFER_LOW_WATER_MARK

ServerBootstrap bootstrap = new ServerBootstrap();

bootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);


bootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);  

For instance, imagine you have a queue of tasks on server side that is filled by clients and processed by backend. In case clients send tasks too quick the length of the queue grows. One needs to introduce so named high watermark and low watermark. If queue length is greater than high watermark stop reading from sockets and queue length will decrease. When queue length becomes less than low watermark start reading tasks from sockets again.

Note, to make it possible for clients to adapt to speed you process tasks (actually to adapt window size) one shouldn't make a big gap between high and low watermarks. From the other side small gap means you'll be too often add/remove sockets from the event loop.

For Netty it seems to be true, because ​​this​​​ JavaDoc for ​​ChannelConfig​

writeBufferHighWaterMark value,Channel.isWritable()

And for low watermark:

Once the number of bytes queued in the write buffer exceeded the high water mark and then dropped down below this value, Channel.isWritable() will return true again.

About sanity, I think, it is relative question, that depends on information that you are sending through the channel and how often. There is no strict rules for what values you must define for that variables. So, I think, you must found your own values in practice. Slides show you one of the examples for that


You can notified for this changes by override the channelWritabilityChanged(...) method in ChannelInboundHandler.


可以通过实现这个接口来监听水位的变化。

水位的变化控制主要在ChannelOutboundBuffer,

private void decrementPendingOutboundBytes(long size, boolean invokeLater) 

 private void incrementPendingOutboundBytes(long size, boolean invokeLater)

这两个方法,一般若不是瞬间水位变高,则会很容易消耗水位,使得其在稳定的水平

​http://dwz.cn/2cQixx​

标签:high,watermark,BUFFER,WATER,读写,queue,水位,low,Netty4
From: https://blog.51cto.com/u_4176761/5973344

相关文章