是否有必要关闭一个 Netty ChunkedInput?

Is it necessary to close a Netty ChunkedInput?

如问题中所述,Netty ChunkedInput 在通过 ChannelHandlerContext.writeAndFlush() 发送数据后是否有必要关闭?我知道这个操作是异步的,所以我可能不应该在调用 writeAndFlush() 后立即关闭它,因此我需要添加一个 FutureListener 来关闭它吗?

在官方的File server例子中,我发现ChunkedInputclose()函数没有被调用:

sendFileFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)), ctx.newProgressivePromise());

是说ChunkedInput读完所有数据就自动关闭了,还是通道真的帮我关闭了?

是的,您几乎不需要自己关闭 ChunkedInput,因为管道中通常会有一个 ChunkedWriteHandler,它会在最后一个块写入后为您关闭它。参见 code

Does it mean that the ChunkedInput is closed automatically when all the data is read

很久以前有一个错误会在读取数据后关闭输入,但已修复为在写入最后一个块后关闭输入。参见 https://github.com/netty/netty/commit/fb52b8a3b2e3940a0bcf6e9167b157d59a811691

在您引用的示例中,管道包含 ChunkedWriteHandler 并且 ChunkedFileChunkedFile 的实现)自动关闭。