Netty4 读写水位控制分析
发布日期:2021-06-30 15:08:04 浏览次数:4 分类:技术文章

本文共 2118 字,大约阅读时间需要 7 分钟。

服务器上看看默认是多少

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  JavaDoc for ChannelConfig says:

If the number of bytes queued in the write buffer exceeds writeBufferHighWaterMark value,Channel.isWritable() will start to return false.

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

转载地址:https://jiangguilong2000.blog.csdn.net/article/details/48109963 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:在Netty底层监控消息发送到Socket的时间
下一篇:Linux内核 RPS/RFS功能详细测试分析

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月07日 16时11分40秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章