Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
“Welcome to the discussion in the comments section. The nuggets will be giving away 100 nuggets in the comments section after the diggnation project. See the event article for details.”
Introduction to the
An important improvement over http1.1 is the flowcontrol flowcontrol. Why flow control? This is because whatever the agreement, the client and the server side when receiving data, there is a temporary storage buffer to temporarily could not handle the data, but the size of the buffer is limited, so the possible situation of buffer overflow, such as the client to the server to upload a large images, the buffer overflow is likely to lead to the server, Some extra packets are lost as a result.
In order to avoid buffer overflows, various HTTP protocols provide certain solutions.
In HTTP1.1, traffic control relies on the underlying TCP protocol, and a buffer is created using the system default Settings when the client and server establish a connection. As data communicates, it is told the size of its receive window, which is the remaining free space in the buffer. If the receive window size is zero, the receiver buffer is full, and the sender will not send data again until the client clears its internal buffer, and then requests to resume the data transfer.
HTTP2 buffer size messages are transmitted between client and server applications, and the application layer controls the flow of data.
This article covers Netty’s support for HTTP2 flow control.
Flow control in HTTP2
As mentioned in the introduction, traditional HTTP1.1 uses the underlying flow control mechanism of the system, specifically TCP flow control. But TCP’s flow control is inadequate in HTTP2. Because HTTP2 uses a multiplexing mechanism, a TCP connection can have multiple HTTP2 connections. So TCP’s own flow control mechanism is too crude for HTTP2.
So in HTTP2, a more sophisticated flow control mechanism is implemented that allows clients and servers to implement their own data flow and connection-level flow control.
When the client establishes a connection with the server, it sends Http2SettingsFrame. The Settings frame contains SETTINGS_INITIAL_WINDOW_SIZE, which is the size of the sending window. Used for Stream level flow control. The default value for the flow control window is set to 65,535 bytes, but the receiver can modify it to a maximum of 2^31-1 bytes.
After the initial Windows size is established, for the receiver, each time the sender sends a Data frame, the size of the window will be reduced, and each time the receiver sends a WINDOW_UPDATE frame, the size of the window will be increased to achieve dynamic control.
Netty’s encapsulation of HTTP2 flow control
Http2FlowController
Http2SettingsFrame = Http2SettingsFrame = Http2SettingsFrame = http2SettingS_initial_WINDOW_size = Http2SettingsFrame; The second aspect is to dynamically increase or decrease the size of the window in subsequent WINDOW_UPDATE frames.
For Netty, this is all wrapped in the Http2FlowController class. Http2FlowController is an abstract class, it has two, respectively is Http2LocalFlowController and Http2RemoteFlowController. They represent the processing of inbound flow of DATA and outbound flow of DATA, respectively.
Http2FlowController has five main methods:
- Set channelHandlerContext: Bind flowControl to channelHandlerContext.
- Set initialWindowSize: Initializes window size, equivalent to setting SETTINGS_INITIAL_WINDOW_SIZE.
- Get initialWindowSize: Returns the initialized Window size.
- WindowSize: Gets the current windowSize.
- IncrementWindowSize: Increases the size of the Flow Control Window.
Let’s take a look at the differences between his two implementation classes.
Http2LocalFlowController
LocalFlowController is used to perform flow control on the DATA frames sent by the remote node. It has five main approaches.
- Set frameWriter: Used to set the frame writer for sending WINDOW_UPDATE frames.
- ReceiveFlowControlledFrame: receive the inbound DATA frame, and on the flow control.
- ConsumeBytes: Indicates that the application has consumed a certain number of bytes and can accept more data sent from the remote node. The flow control can send a WINDOW_UPDATE frame to reset the window size.
- UnconsumedBytes: Bytes received but not consumed.
- InitialWindowSize: the initialWindowSize of the given stream.
Http2RemoteFlowController
RemoteFlowController is used to handle outbound DATA Frames sent to remote nodes. It provides eight methods:
- Get channelHandlerContext: Gets the current flow control context.
- AddFlowControlled: Adds the flow control payload to the queue sent to the remote node.
- HasFlowControlled frames: checks whether the stream hasFlowControlled frames in its queue.
- WritePendingBytes: Writes all pending data in the traffic controller to the traffic control limit.
- Listener: Adds a listener to the flow-controller.
- IsWritable: Determines whether the stream has any bytes left for the flow control window.
- Whether channelWritabilityChanged: the context of the writable state changes.
- UpdateDependencyTree: Updates the dependencies between streams because streams can have a parent-child structure.
Use of flow control
FlowControl classes are used in Http2Connection, Http2ConnectionDecoder, and Http2ConnectionEncoder to establish an HTTP2 connection.
conclusion
FlowControl flowControl is a relatively low-level concept in HTTP2 that you will encounter in your in-depth understanding of Netty’s HTTP2 implementation.
This article is available at www.flydean.com/29-netty-fl…
The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!
Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!