简体   繁体   English

HTTP2:如何实现流量控制功能?

[英]HTTP2: How to implement flow control feature?

I'm studying some features of http2, one of them is Flow Control and want to understand that How can I implement it .我正在研究 http2 的一些功能,其中之一是流量控制,想了解如何实现它

If can, give me an example or demo.如果可以,给我一个例子或演示。 Thanks.谢谢。

I implemented HTTP/2 in Jetty (a Java HTTP and WebSocket server), so I can point you to how it has been implemented in Jetty - but you can look at other open source projects that implement HTTP/2 and look how they have done it.我在Jetty (一个 Java HTTP 和 WebSocket 服务器)中实现了 HTTP/2,所以我可以向你指出它是如何在 Jetty 中实现的——但你可以看看其他实现 HTTP/2 的开源项目,看看他们是如何做的它。

The Jetty implementation is based around class FlowControlStrategy . Jetty 实现基于类FlowControlStrategy

There are two implementations, one naive (SimpleFlowControlStrategy) and one more efficient (BufferingFlowControlStrategy).有两种实现方式,一种是简单的 (SimpleFlowControlStrategy),另一种是更高效的 (BufferingFlowControlStrategy)。

In both cases the FlowControlStrategy receives events from the HTTP/2 implementation, in particular:在这两种情况下, FlowControlStrategy 从 HTTP/2 实现接收事件,特别是:

  1. when a DATA frame is sent to the other peer当一个 DATA 帧被发送到另一个对等方时
  2. when a WINDOW_UPDATE frame is received from the other peer当从另一个对等方接收到 WINDOW_UPDATE 帧时
  3. when a DATA frame is received from the other peer当从另一个对等方接收到数据帧时
  4. when the received data is consumed by the application当应用程序使用接收到的数据时

In case of data sent to the other peer, the "send" flow control window is decreased;如果数据发送到另一个对等点,“发送”流量控制窗口减少; when a WINDOW_UPDATE frame is received from the other peer the "send" flow control window is increased.当从另一个对等方接收到 WINDOW_UPDATE 帧时,“发送”流控制窗口会增加。

Similarly, when data is received the "receive" flow control window is decreased;类似地,当接收到数据时,“接收”流控制窗口减小; when the application consumes the received data, the "receive" flow control window is increased - and possibly a WINDOW_UPDATE frame is sent to the other peer to signal that it can send more data.当应用程序使用接收到的数据时,“接收”流控制窗口会增加 - 并且可能会向其他对等方发送 WINDOW_UPDATE 帧以表示它可以发送更多数据。

The HTTP/2 implementation needs to check these two flow control windows and stop sending data when the "send" flow control window reaches zero (or negative); HTTP/2 实现需要检查这两个流控窗口,并在“发送”流控窗口达到零(或负)时停止发送数据; and fail the connection if it receives data when the "receive" flow control window is zero (or negative).如果在“接收”流控制窗口为零(或负)时接收到数据,则连接失败。 Upon receiving aa WINDOW_UPDATE frame, the HTTP/2 implementation needs to resume sending data.收到 WINDOW_UPDATE 帧后,HTTP/2 实现需要恢复发送数据。

This is the basic of how flow control in HTTP/2 should be implemented.这是如何在 HTTP/2 中实现流量控制的基础。 Depending on the technology used and the implementation details, there are many more things to take care of such as data queuing, data copying, thread safety, etc. but you can address those while you are writing the implementation.根据所使用的技术和实现细节,还有很多事情需要处理,例如数据排队、数据复制、线程安全等。但是您可以在编写实现时解决这些问题。

Have fun!玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM