使用Netty 4.1进行内存感知通道处理

时间:2017-06-15 08:17:36

标签: java netty

在4(3.x)之前的Netty版本中,有一种方法可以通过执行程序识别内存来进行通道处理,并使用OrderedMemoryAwareThreadPoolExecutor执行程序对其进行排序以执行给定{{}的操作1}}。 3.x中的Channel将负责为通道排序事件处理,即使它们可以由不同的线程执行,也可以限制OrderedMemoryAwareThreadPoolExecutor使用的总内存。如果通道内存(由于排队事件)超过某个阈值,则会阻止事件的执行,直到释放内存为止。

然而,在4.x中,没有这样的机制。新的线程模型确实提供了对已执行事件的排序(因为特定通道的事件由单个线程执行),但似乎没有办法限制单个Channel中任何一个消耗的内存Channel。这意味着,如果这是不可能的,发送到一个特定EventExecutorGroup的很多事件可能耗尽服务器上的内存。虽然我还没有对此事进行测试,但我认为在这里询问Netty 4.x的情况是否真的值得一提。

所以我的问题基本上是:

在Netty 4.x中使用Channel Channel时,有没有办法限制单个EventExecutorGroup占用的内存?

1 个答案:

答案 0 :(得分:3)

You are right. This kind of the situation is possible.

However, Netty has ChannelOption.WRITE_BUFFER_WATER_MARK option for your channel. So when you writing too fast into some channel and queue of pending messages exceeds ChannelOption.WRITE_BUFFER_WATER_MARK limit the channel you are writing to will become not writable. So you can guard your code with:

if (channel.isWritable()) {
}

or

if (ctx.channel().isWritable()) {
}

And thus prevent exhausting of memory when the channel is busy or consumes events slowly.

You also can change ChannelOption.AUTO_READ for channel that generates events and handles this manually with:

 ctx.channel().config().setAutoRead(false);

So your server will stop read events from the channel that generates them too much. Here is pull request that demonstrates this way.