如何关闭写入通道
我们使用的是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.