[英]Netty: How can I reuse the same `FullHttpResponse`?
在我的項目中,我想為許多客戶編寫相同的FullHttpResponse
以提高性能。
以前我為自定義協議編寫了相同的ByteBuf
,並使用retain
來防止寫入后釋放buf。
不幸的是,使用FullHttpResponse
( DefaultFullHttpResponse
),我的技術似乎不起作用。 我第一次寫響應時客戶端正確收到響應,但下一次寫操作沒有通過。
我做了一個簡單的System.out.println
測試,以確保沒有阻塞,我的代碼完全執行,我的測試顯示是,沒有任何阻塞,請求似乎確實通過。
我正在使用Maven Central發布的Netty 4.1.0.Final
。
管道只有一個HttpServerCodec(256, 512, 512, false, 64)
FullHttpResponse
HttpServerCodec(256, 512, 512, false, 64)
和我的SimpleChannelInboundHandler<HttpRequest>
,我發送了FullHttpResponse
。
這是我的入站處理程序的簡化版本:
class HTTPHandler extends SimpleChannelInboundHandler<HttpRequest> {
private static final FullHttpResponse response =
new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.OK,
Unpooled.buffer(8).writeLong(0),
false);
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) {
ctx.writeAndFlush(response.retain(), ctx.voidPromise());
}
}
您需要使用response.duplicate().retain()
或者如果使用Netty 4.1.x,您還可以使用response.retainedDuplicate()
。
這是確保您獲得單獨的讀取器/寫入器索引所必需的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.