HttpRequest和LastHttpContent之间的时间很长

时间:2016-02-17 10:30:52

标签: http netty

我是netty的新手,我正在尝试构建一个侦听来自单个客户端的传入数据的HTTP服务器。 HTTP请求大约是200Kb,我们得到大约10 /秒。

我的问题是响应这些请求的延迟非常高(差不多8秒),有些请求完全超时,我的AWS负载均衡器显示5XX响应。

下面是我的设置,以及为什么延迟可能如此之高的想法。通过查看wireshark跟踪,我可以看到请求的TCP段相距很远。

服务器

public void run() {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch)
                            throws Exception {

                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new HttpRequestDecoder());
                        p.addLast(new HttpResponseEncoder());
                        p.addLast(new FacebookHttpServerHandler());
                    }

                }).option(ChannelOption.SO_KEEPALIVE, true)
                .option(ChannelOption.TCP_NODELAY, true)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
                .option(ChannelOption.SO_TIMEOUT, 100)
                .option(ChannelOption.SO_BACKLOG, 1024);

        // Bind and start to accept incoming connections.
        ChannelFuture f = b.bind(port).sync();

        log.info("before shutdown server...");

        // Wait until the server socket is closed.
        // shut down your server.
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        log.error("unexpected exception", e);
    }

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            log.info("shutdown gracefully...");
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    });
}

public static void main(String[] args) throws Exception {
    String host = "localhost";
    int port;
    if (args.length > 0) {
        host = args[0];
        port = Integer.parseInt(args[1]);
    } else {
        port = 8080;
    }

    log.info("host :" + host + " ,  port : " + port);
    new FacebookCollector(host, port).run();
}

处理程序

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        HttpRequest request = this.request = (HttpRequest) msg;

        if (HttpUtil.is100ContinueExpected(request)) {
            send100Continue(ctx);
        }
    }

    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;

        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            buf.append(content.toString(CharsetUtil.UTF_8));
            // TODO appendDecoderResult(buf, request);
        }

        if (msg instanceof LastHttpContent) {
            LastHttpContent trailer = (LastHttpContent) msg;

            processFeed(buf.toString());

            if (!writeResponse(trailer, ctx)) {
                // If keep-alive is off, close the connection once the
                // content is fully written.
                ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(
                        ChannelFutureListener.CLOSE);
            }
        }
    }
}

非常感谢!

0 个答案:

没有答案