如何关闭写入通道

发布于 2024-12-06 07:20:05 字数 350 浏览 2 评论 0原文

我们使用的是Netty 3.2.3版本,想要根据一定的计算关闭通道的写入。

我们正在通过以下方式关闭通道:

channel.setInterestOps(channel.getInterestOps() | Channel.OP_WRITE);

并通过以下方式重新打开通道:

channel.setInterestOps(channel.getInterestOps() & Channel.OP_WRITE);

但根据性能,出现了问题。 计算与我们进行 serReadable true/false 的计算相同, 工作正常。

We are using Netty 3.2.3 version and want to close the channel from writing according to a certain calculation.

We are closing the channel by:

channel.setInterestOps(channel.getInterestOps() | Channel.OP_WRITE);

and re-opening it by:

channel.setInterestOps(channel.getInterestOps() & Channel.OP_WRITE);

But according to the performance something is wrong.
the calculation is the same as we are doing serReadable true/false,
that is working fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自我难过 2024-12-13 07:20:05

Channel.OP_WRITE 仅供参考。 Netty 使用它来指示 I/O 线程是否可以立即将数据写入通道。如果channel.isWritable()为假,那么Netty将对任何进一步的数据进行排队,直到它可以被通道接受。如果在 isWritable() 为 false 时继续写入数据,那么最终可能会遇到 OutOfMemoryError。

我假设您不想真正关闭通道,只需停止写入即可。正确的方法是在每次发送后检查channel.isWritable(),或者接收ChannelStateEvent(可能通过重写SimpleChannelUpstreamHandler.channelInterestChanged())并暂停正在将数据写入通道的任何应用程序进程。

无论哪种情况,您都需要接收 ChannelStateEvent 以了解何时可以再次开始写入通道。

Channel.OP_WRITE is for information only. Netty uses it to signal whether the I/O thread can write data to the channel immediately. If channel.isWritable() is false then Netty will queue any further data until it can be accepted by the channel. If you continue to write data when isWritable() is false then you may eventually encounter an OutOfMemoryError.

I assume you don't want to actually close the channel, just stop writing to it. The correct approach is to either check channel.isWritable() after each send, or receive ChannelStateEvent (possibly by overriding SimpleChannelUpstreamHandler.channelInterestChanged()) and pause whatever application process you have that is writing data to the channel.

In either case you'll want to receive ChannelStateEvent to know when you can start writing to the channel again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文