使用 Netty 4,如何检测我写入的数据在我的连接端被堵塞,并在远程端无法跟上时关闭连接?
我正在检查是否
ChannelHandlerContext.channel.outboundByteBuffer.readableBytes > MaxWriteBuffer
但在最新的 Netty 4 版本中,这给了我一个异常(exception):
java.lang.IllegalStateException: nextOutboundByteBuffer() called from outside the eventLoop
最佳答案
您需要从 EventLoop 执行此操作,否则它不是线程安全的。
所以像这样:
ChannelHandlerContext ctx = ....
final Channel channel = ctx.channel();
channel.eventLoop().execute(new Runnable() {
public void run() {
if (channel.outboundByteBuffer.readableBytes() > MaxWriteBuffer) {
// do something
}
}
});
关于netty - 如何在 Netty 4 中删除拥塞的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16368572/