前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊reactor-netty的AccessLogHandlerH2

聊聊reactor-netty的AccessLogHandlerH2

原创
作者头像
code4it
发布2019-04-05 23:56:38
6630
发布2019-04-05 23:56:38
举报
文章被收录于专栏:码匠的流水账码匠的流水账

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

AccessLogHandlerH2

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • AccessLogHandlerH2
  • HttpServerBind
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档