7

netty对http2协议的解析

 2 years ago
source link: https://qiankunli.github.io/2017/06/12/netty_http2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

本文从源码角度分析netty-codec-http2对http2协议的实现,netty 对 http1.1的实现参见netty对http协议解析原理(一)

http2协议

http那些事

netty实现

对外的使用接口

http2本身一个复杂的协议,有着自己的一套“复杂的类图”,那么netty 与 netty-http2如何结合?

  1. 配置Http2ConnectionHandler 负责decode数据,decode时会触发Http2FrameListener的执行
  2. 自定义Http2FrameListener,并与Http2ConnectionHandler关联
  3. 使用Http2ConnectionEncoder.writeXX发送frame,write方法的执行要传入ctx对象。

前文提过,http2通信,就是收发一个个Http2Frame。在代码层面上,接口也是围绕各个类型的frame的onXXRead和writeXXX来进行的。

上层接口关系如下

主要的点如下

  1. Http2ConnectionHandler extends ByteToMessageDecoder,整个读的流程由ByteToMessageDecoder.onDecode方法驱动。
  2. Http2FrameReader和Http2FrameWriter只是单纯的负责读写frame,Http2FrameReader.readFrame(ChannelHandlerContext ctx, ByteBuf input, Http2FrameListener listener) throws Http2Exception;中有一个listener成员,读取的frame会根据类型的不同,触发listener onXXRead方法的执行。Http2FrameWriter负责各类frame的写入。
  3. frame包括数据frame和控制frame

    • 读取到控制frame时,要更新本地控制数据,比如收到window update frame
    • 读取到控制frame时,要对远端控制指定做出一定的反应,比如收到end frame of stream 或者 rst frame,即需要关闭stream,清除本地的stream数据(这里工作由Http2LifecycleManager负责)
    • write数据时,要考虑本地控制model的实际情况,比如流控。
    • write数据时,要对调用方控制指令做出一定的反应,比如调用方发送了end frame of stream

    这也是Http2ConnectionHandler没有直接聚合Http2FrameReader和Http2FrameWriter,而是另提一个Http2ConnectionDecoder、Http2ConnectionEncoder的原因。同时,因为读写逻辑的任务不同,其代码组织也就稍有不同。

从中学到的:

  1. 接口只能指定方法,这个方法可以描述一个功能,也可以描述一个实现类应该具备哪些成员。
  2. 接口只是描述一个角色,而类可以根据自己实现这个角色的便捷性(比如具备所有相关的能力对象),实现多个角色。
  3. 面向过程,是从驱动代码的开始处(一般是main方法,此处则是eventloop及其驱动的onDecode方法),线性的实现逻辑。面向对象,则是抛开驱动逻辑,分析业务应该具备哪些对象,对象之间的关系是怎样的。对象和线程通常是撕裂的。

明确整体结构,还有两个主线

  1. netty 和 netty-http2如何交互
  2. netty-http2 和频控组件如何交互

Http2ConnectionHandler extends ByteToMessageDecoder.onDecode ==> Http2ConnectionDecoder.decodeFrame ==> Http2FrameReader. readFrame(ChannelHandlerContext ctx, ByteBuf input, Http2FrameListener listener) ==> Http2FrameListener 回调函数

netty对接收数据的抽象,基本上就是各种帧的监听事件。

interface Http2FrameListener{
	int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding,
               boolean endOfStream) throws Http2Exception;
   void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding,
        boolean endOfStream) throws Http2Exception;
   void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency,
        short weight, boolean exclusive) throws Http2Exception;
   void onRstStreamRead(ChannelHandlerContext ctx, int streamId, long errorCode) throws Http2Exception;
   void onSettingsAckRead(ChannelHandlerContext ctx) throws Http2Exception;
   void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings) throws Http2Exception;
   void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception;
   void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception;
   void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId,
        Http2Headers headers, int padding) throws Http2Exception;
   void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData)
        throws Http2Exception;
   void onWindowUpdateRead(ChannelHandlerContext ctx, int streamId, int windowSizeIncrement)
        throws Http2Exception;
   void onUnknownFrame(ChannelHandlerContext ctx, byte frameType, int streamId, Http2Flags flags, ByteBuf payload)
        throws Http2Exception;
}

一般情况下

  1. 用户处理onHeadersRead和onDataRead等业务数据
  2. netty http2处理onWindowUpdateRead和onGoAwayRead等控制数据

同时,提供Http2Connection、Http2Stream来存储对应帧、Stream的上下文数据。

DefaultHttp2Connection Http2Connection{
	 IntObjectMap<Http2Stream> streamMap = new IntObjectHashMap<Http2Stream>();
	 DefaultEndpoint<Http2LocalFlowController> localEndpoint;
	 DefaultEndpoint<Http2RemoteFlowController> remoteEndpoint;
	 List<Listener> listeners = new ArrayList<Listener>(4);
	 Promise<Void> closePromise;
}
  1. 存储连接上的Http2Stream
Http2FrameWriter{
	 ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
                           int padding, boolean endStream, ChannelPromise promise);
    ChannelFuture writePriority(ChannelHandlerContext ctx, int streamId, int streamDependency,
        short weight, boolean exclusive, ChannelPromise promise);
   ...
}
  1. 非data数据直接发送,this.encoder().writeHeaders ==> Http2FrameWriter.writeHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endStream, ChannelPromise promise)
  2. data数据由流控组件负责发送,this.encoder().writeData ==> encoder.flowController().addFlowControlled

所以流控是一个大头

流控是双向的,根据远程的window update更新流控数据,同时,根据消费数据以及本地空间发送window update。本文主要侧重于前者。

即便window size富余,流控组件也只是write数据,不会write flush数据。

流控组件的接口

流控组件Http2FlowController与外部的接口

  1. encoder.writeData() ==> flowController().addFlowControlled 将数据加入内部队列。可以看到,这里并没有真正写数据。

  2. 何时真正写数据,写操作时,会根据window size是否富余,来判断是否实际进行读写。:

    • Http2ConnectionHandler extends ByteToMessageDecoder implements ChannelOutBoundHandler
    • ByteToMessageDecoder extends ChannelInboundHandlerAdapter
    • Http2ConnectionHandler 覆盖了ChannelOutBoundHandler的flush方法,执行encoder.flowController().writePendingBytes();
    • Http2ConnectionHandler 覆盖了ChannelInboundHandlerAdapter 的channelWritabilityChanged、channelReadComplete方法,触发flush方法的执行,执行encoder.flowController().writePendingBytes();
  3. 更新window size

    DefaultHttp2ConnectionDecoder 本身内置一个 Http2FrameListener,decoder回调onWindowUpdateRead,执行encoder.flowController().incrementWindowSize(stream, windowSizeIncrement);

流控组件的发送逻辑

首先,连接有一个整体的window size,每个stream也有自己的window size。

encoder.flowController().writePendingBytes(); ==> streamByteDistributor.distribute(bytesToWrite, writer)触发的是整个连接的发送,在整个连接window size 富余的前提下,从连接里拿出还有富余window size的stream,streamByteDistributor确定stream此次的numBytes,由writer负责实际的ctx.write。

对于每一个stream,有两个state,state对stream做进一步封装

  1. 一个StremByteDistributor.StreamState负责记录它的window size,pending bytes。尽管caller调用组件发送帧,但流控组件按byte发送数据,数量由streamByteDistributor确定。
  2. 一个UniformStreamByteDistributor.State/WeightedFairQueueByteDistributor.State,该state作为载体 ,存储stream及优先级数据。该state存储在个UniformStreamByteDistributor/WeightedFairQueueByteDistributor的队列中。

比如采用公平策略的UniformStreamByteDistributor,假设connection window size = 1000,stream window size =120,该connection 一共10个stream,则本次encoder.flowController().writePendingBytes(),该stream可以发送min(120,1000/10)个字节的数据。在实际情况下,还要考虑channel 写缓冲区的大小。

收到一个window update frame,接收方处理该frame100的数据。则一方面会更新对应stream的window size,加100,也会更新conneciton window size,加100。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK