聊聊reactor-netty的AccessLogHandlerH2

栏目: 编程工具 · 发布时间: 5年前

内容简介:本文主要研究一下reactor-netty的AccessLogHandlerH2reactor-netty-0.8.5.RELEASE-sources.jar!/reactor/netty/http/server/AccessLogHandlerH2.javaAccessLogHandlerH2是HTTP2的access log的实现,具体针对Http2HeadersFrame及Http2DataFrame进行了判断

本文主要研究一下reactor-netty的AccessLogHandlerH2

AccessLogHandlerH2

reactor-netty-0.8.5.RELEASE-sources.jar!/reactor/netty/http/server/AccessLogHandlerH2.java

final class AccessLogHandlerH2 extends ChannelDuplexHandler {
    static final String H2_PROTOCOL_NAME = "HTTP/2.0";

    AccessLog accessLog = new AccessLog();

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof Http2HeadersFrame){
            final Http2HeadersFrame requestHeaders = (Http2HeadersFrame) msg;
            final SocketChannel channel = (SocketChannel) ctx.channel()
                                                             .parent();
            final Http2Headers headers = requestHeaders.headers();

            accessLog = new AccessLog()
                    .address(channel.remoteAddress().getHostString())
                    .port(channel.localAddress().getPort())
                    .method(headers.method())
                    .uri(headers.path())
                    .protocol(H2_PROTOCOL_NAME);
        }
        super.channelRead(ctx, msg);
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        boolean lastContent = false;
        if (msg instanceof Http2HeadersFrame) {
            final Http2HeadersFrame responseHeaders = (Http2HeadersFrame) msg;
            final Http2Headers headers = responseHeaders.headers();
            lastContent = responseHeaders.isEndStream();

            accessLog.status(headers.status())
                     .chunked(true);
        }
        if (msg instanceof Http2DataFrame) {
            final Http2DataFrame data = (Http2DataFrame) msg;
            lastContent = data.isEndStream();

            accessLog.increaseContentLength(data.content().readableBytes());
        }
        if (lastContent) {
            ctx.write(msg, promise)
               .addListener(future -> {
                   if (future.isSuccess()) {
                       accessLog.log();
                   }
               });
            return;
        }
        ctx.write(msg, promise);
    }
}

AccessLogHandlerH2是HTTP2的access log的实现,具体针对Http2HeadersFrame及Http2DataFrame进行了判断

HttpServerBind

reactor-netty-0.8.5.RELEASE-sources.jar!/reactor/netty/http/server/HttpServerBind.java

final class HttpServerBind extends HttpServer
        implements Function<ServerBootstrap, ServerBootstrap> {

    //......
        
    static void addStreamHandlers(Channel ch, ConnectionObserver listener, boolean readForwardHeaders,
            ServerCookieEncoder encoder, ServerCookieDecoder decoder) {
        if (ACCESS_LOG) {
            ch.pipeline()
              .addLast(NettyPipeline.AccessLogHandler, new AccessLogHandlerH2());
        }
        ch.pipeline()
          .addLast(new Http2StreamBridgeHandler(listener, readForwardHeaders, encoder, decoder))
          .addLast(new Http2StreamFrameToHttpObjectCodec(true));

        ChannelOperations.addReactiveBridge(ch, ChannelOperations.OnSetup.empty(), listener);

        if (log.isDebugEnabled()) {
            log.debug(format(ch, "Initialized HTTP/2 pipeline {}"), ch.pipeline());
        }
    }

    //......

HttpServerBind的addStreamHandlers静态方法用于判断是否开启access log,开启的话会创建AccessLogHandlerH2并添加到pipeline;Http1OrH2CleartextCodec的initChannel方法以及Http2StreamInitializer的initChannel方法均调用到了此方法

小结

  • AccessLogHandlerH2是HTTP2的access log的实现,具体针对Http2HeadersFrame及Http2DataFrame进行了判断
  • HttpServerBind的addStreamHandlers静态方法用于判断是否开启access log,开启的话会创建AccessLogHandlerH2并添加到pipeline
  • Http1OrH2CleartextCodec的initChannel方法以及Http2StreamInitializer的initChannel方法均调用到了此方法

doc


以上所述就是小编给大家介绍的《聊聊reactor-netty的AccessLogHandlerH2》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java并发编程实战

Java并发编程实战

Brian Goetz、Tim Peierls、Joshua Bloch、Joseph Bowbeer、David Holmes、Doug Lea / 童云兰 / 机械工业出版社华章公司 / 2012-2 / 69.00元

本书深入浅出地介绍了Java线程和并发,是一本完美的Java并发参考手册。书中从并发性和线程安全性的基本概念出发,介绍了如何使用类库提供的基本并发构建块,用于避免并发危险、构造线程安全的类及验证线程安全的规则,如何将小的线程安全类组合成更大的线程安全类,如何利用线程来提高并发应用程序的吞吐量,如何识别可并行执行的任务,如何提高单线程子系统的响应性,如何确保并发程序执行预期任务,如何提高并发代码的性......一起来看看 《Java并发编程实战》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

MD5 加密
MD5 加密

MD5 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具