Netty PooledByteBuffAllocator';如果从ChannelHandlerContext分配,则会在HttpServerCodeHandler中释放缓冲区?
我正在使用PooledByteBufAllocator和Direct Pool are true分配缓冲区,这是在服务器安装期间在通道中指定的分配器。我使用这个分配器来创建响应。我是否需要在响应中释放此缓冲区,或者此响应缓冲池的释放将在HttpServerCodec中处理?我这样做是为了获得性能优势和更少的gc 管道代码:Netty PooledByteBuffAllocator';如果从ChannelHandlerContext分配,则会在HttpServerCodeHandler中释放缓冲区?,netty,Netty,我正在使用PooledByteBufAllocator和Direct Pool are true分配缓冲区,这是在服务器安装期间在通道中指定的分配器。我使用这个分配器来创建响应。我是否需要在响应中释放此缓冲区,或者此响应缓冲池的释放将在HttpServerCodec中处理?我这样做是为了获得性能优势和更少的gc 管道代码: pipeline.addLast("decoderEncoder", new HttpServerCodec()); pipeline.addLast("aggregator
pipeline.addLast("decoderEncoder", new HttpServerCodec());
pipeline.addLast("aggregator", new HttpObjectAggregator(1024 * 1024));// 1 MB data size
pipeline.addLast("handler", httpRequestHandlerProvider.get());
服务器引导代码:
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(8800)).childHandler(serverChannelInitializer)
// disable nagle's algorithm
.childOption(ChannelOption.TCP_NODELAY, true)
// allow binding channel on same ip, port
.childOption(ChannelOption.SO_REUSEADDR, true).childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true)).bind()
.sync();
在响应代码中,在我的自定义“处理程序”-httpRequestHandler中:
private void sendResp(final String responseString, final ChannelHandlerContext ctx) {
byte[] bytes = responseString.getBytes(Charsets.UTF_8);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
ctx.alloc().buffer(bytes.length).writeBytes(bytes), false);
}
如果您写了回复,Netty会小心地为您释放缓冲区。谢谢!读你的书,太棒了。案例研究也很好。