Netty Splice 操作在 channelRead 中不起作用

Netty Splice operation not working in channelRead

提问人:nakulmatta 提问时间:11/7/2023 更新时间:11/7/2023 访问量:17

问:

  • 我正在使用拼接操作将数据从一个入站通道传输到出站通道,反之亦然,使用拼接操作来提高性能。
  • 入站通道位于客户端和代理服务之间,出站通道位于代理和后端服务之间。
  • 数据应该使用 Splice 操作在 channelRead() 中传输,而不是在 channelActive() 中传输,因为我们需要在 1 个处理程序的 channelRead() 中解析目标服务器地址。
  • 在通道设置时,我们不知道目标服务器地址,并且基于请求内容

本身,这就是为什么它是在 channelRead() 中完成的

问题是拼接操作仅在 channelActive() 中有效,但在 channelRead() 中使用时,操作会卡住并且请求不会转到目标服务器

这是我的代码

            if (useSplice) {
                 final EpollSocketChannel source = (EpollSocketChannel) ctx.channel();
                 final EpollSocketChannel destination = (EpollSocketChannel) outboundChannel;
     
                 source.config().setAutoRead(false);
     
                 source.eventLoop().execute(() -> source.spliceTo(destination, Integer.MAX_VALUE)
                         .addListener((ChannelFutureListener) future -> {
                             if (!future.isSuccess()) {
                                 future.channel().close();
                             }
                         }));
     
                 source.config().setAutoRead(true);
            }

如果我在拼接操作后添加 writeAndFlush(),它会取消阻止拼接操作并且服务器不会挂起。在此之后,还观察到了非常好的性能改进。

         outboundChannel.writeAndFlush(msg).addListener((ChannelFutureListener) future -> {
                 if (future.isSuccess()) {
                     // was able to flush out data, start to read the next chunk
                 ctx.channel().read();
             } else {
                 log.error("Failed writing data.", future.cause());
                 future.channel().close();
             }
         });

问题:

  • 这是在 channelRead() 中使用拼接的预期方式吗?
  • 拼接可以在处理程序链的中间使用吗?

我在 Netty 4.1 上使用 JDK 11

netty 拼接 netty4

评论


答: 暂无答案