Netty 过滤反向代理

3
我正在尝试使用Netty 4.1编写高性能反向代理服务器。 我的代码基于Feng-Zihao/protox的Java适配版本和Netty Proxy Example
起初,我在处理100-CONTINUE时遇到了一些问题,但将HttpObjectAggregator添加到我的管道中解决了这个问题。
    serverBootstrap
        .group(bossGroup, workerGroup)
        .channel(NioServerSocketChannel.class)
        .handler(new LoggingHandler(LogLevel.DEBUG))
        .childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                ch.pipeline().addLast(new HttpRequestDecoder());
                ch.pipeline().addLast(new HttpResponseEncoder());
                ch.pipeline().addLast(new HttpObjectAggregator(1048576));
                ch.pipeline().addLast(new FrontendHandler());
            }
        })
        //          .option(ChannelOption.SO_REUSEADDR, true)
        //          .option(ChannelOption.SO_BACKLOG, 128)
        //          .childOption(ChannelOption.SO_KEEPALIVE, true)
        .childOption(ChannelOption.AUTO_READ, false)
        .bind(port).sync();

在客户端,请求会无限期地挂起。 问题是,AUTO_READfalse 似乎阻止了 HttpObjectAggregator 的工作,而我的 FrontendHandler 只接收到 channelActive 事件,但从未接收到 channelRead 事件。
看起来我需要这样做,以确保不会出现读取和远程对等连接之间的竞争条件。
FYI,我的最终目标是根据过滤器(可能是在我的 FrontendHandler 之前的新处理程序)选择是否转发请求,该过滤器将需要读取完整的 HTTP 内容。
我错过了什么吗?
1个回答

3

当你的出站通道变为活动状态时,打开自动读取功能,并在处理每个消息时将其关闭。然后在准备处理另一条消息时再次打开它。

这将使HttpObjectAggregator继续读取尽可能多的消息以创建FullHttpMessage,然后在FrontendHandler处理或等待某个客户端写入来调用监听器时停止发送消息。

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ctx.channel().config().setAutoRead(false);
    ...
    // This call should probably be in some event listener
    ctx.channel().config().setAutoRead(true);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接