跳到主要内容

5. Streams and Multiplexing

"stream" 是在 HTTP/2 connection 内, client 与 server 之间交换的一组独立的双向 frame 序列.

关键特性

  • Streams 可以由 client 或 server 单方面建立和使用, 也可以由双方共享
  • Streams 可以由任一 endpoint 关闭
  • 在一个 stream 上发送的 frame 的顺序具有语义意义
  • Streams 由一个整数标识 (Stream Identifier)
  • client 发起的 Stream Identifiers 为奇数; server 发起的为偶数
  • Stream ID 0 保留用于 connection control messages

5.1 Stream States

stream 在其生命周期中会经历多种状态转换:

                         +--------+
send PP | | recv PP
,--------| idle |--------.
/ | | \
v +--------+ v
+----------+ | +----------+
| | | send H / | |
,------| reserved | | recv H | reserved |------.
| | (local) | | | (remote) | |
| +----------+ v +----------+ |
| | +--------+ | |
| | recv ES | | send ES | |
| send H | ,-------| open |-------. | recv H |
| | / | | \ | |
| v v +--------+ v v |
| +----------+ | +----------+ |
| | half | | | half | |
| | closed | | send R / | closed | |
| | (remote) | | recv R | (local) | |
| +----------+ | +----------+ |
| | | | |
| | send ES / | recv ES / | |
| | send R / v send R / | |
| | recv R +--------+ recv R | |
| send R / `----------->| |<-----------' send R / |
| recv R | closed | recv R |
`----------------------->| |<----------------------'
+--------+

5.2 Flow Control

HTTP/2 使用 WINDOW_UPDATE frames 提供 flow control, 以防止 sender 压垮 receiver.

原则:

  • Flow control 特定于一个 connection
  • Flow control 是有方向的
  • Flow control 基于 credit
  • 初始 flow-control window size 为 65,535 octets
  • Flow control 适用于 DATA frames

5.3 Stream Priority

Clients 可以使用 PRIORITY frames, 或带有 priority information 的 HEADERS frames, 为 streams 分配 priority.