Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 892fc35

Browse files
committedAug 7, 2022
http2: send client conn flow control bytes back immediately
HTTP/2 server must return client connection flow control bytes back immediately on receiving data frame. Connection flow control updates must not depend on whether data has been read by application handler or not. This prevents fast streams from starving due to slow streams.
1 parent a33c5aa commit 892fc35

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed
 

‎http2/server.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -1772,10 +1772,18 @@ func (sc *serverConn) processData(f *DataFrame) error {
17721772
st.bodyBytes += int64(len(data))
17731773
}
17741774

1775+
// Connection level flow control window update must be
1776+
// sent immediately after receiving data frame.
1777+
// This separates connection's flow control from body reads
1778+
// as connection's flow control must not depend on whether
1779+
// body has been read by application handler or not.
1780+
// This prevents fast streams from starving due to other
1781+
// slow streams.
1782+
sc.sendWindowUpdate32(nil, int32(f.Length))
1783+
17751784
// Return any padded flow control now, since we won't
17761785
// refund it later on body reads.
17771786
if pad := int32(f.Length) - int32(len(data)); pad > 0 {
1778-
sc.sendWindowUpdate32(nil, pad)
17791787
sc.sendWindowUpdate32(st, pad)
17801788
}
17811789
}
@@ -2317,7 +2325,6 @@ func (sc *serverConn) noteBodyReadFromHandler(st *stream, n int, err error) {
23172325

23182326
func (sc *serverConn) noteBodyRead(st *stream, n int) {
23192327
sc.serveG.check()
2320-
sc.sendWindowUpdate(nil, n) // conn-level
23212328
if st.state != stateHalfClosedRemote && st.state != stateClosed {
23222329
// Don't send this WINDOW_UPDATE if the stream is closed
23232330
// remotely.

‎http2/server_test.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -1251,19 +1251,21 @@ func TestServer_Handler_Sends_WindowUpdate(t *testing.T) {
12511251
EndHeaders: true,
12521252
})
12531253
st.writeData(1, false, []byte("abcdef"))
1254+
// Expect to immediately get connection level 6 bytes back.
1255+
st.wantWindowUpdate(0, 6)
1256+
12541257
puppet.do(readBodyHandler(t, "abc"))
1255-
st.wantWindowUpdate(0, 3)
12561258
st.wantWindowUpdate(1, 3)
12571259

12581260
puppet.do(readBodyHandler(t, "def"))
1259-
st.wantWindowUpdate(0, 3)
12601261
st.wantWindowUpdate(1, 3)
12611262

12621263
st.writeData(1, true, []byte("ghijkl")) // END_STREAM here
1264+
st.wantWindowUpdate(0, 6)
1265+
1266+
// no more stream-level window updates, since END_STREAM
12631267
puppet.do(readBodyHandler(t, "ghi"))
12641268
puppet.do(readBodyHandler(t, "jkl"))
1265-
st.wantWindowUpdate(0, 3)
1266-
st.wantWindowUpdate(0, 3) // no more stream-level, since END_STREAM
12671269
}
12681270

12691271
// the version of the TestServer_Handler_Sends_WindowUpdate with padding.
@@ -1286,17 +1288,17 @@ func TestServer_Handler_Sends_WindowUpdate_Padding(t *testing.T) {
12861288
})
12871289
st.writeDataPadded(1, false, []byte("abcdef"), []byte{0, 0, 0, 0})
12881290

1289-
// Expect to immediately get our 5 bytes of padding back for
1290-
// both the connection and stream (4 bytes of padding + 1 byte of length)
1291-
st.wantWindowUpdate(0, 5)
1291+
// Expect to immediately get our 6 bytes of data + 5 bytes of padding
1292+
// back at connection level.
1293+
// (4 bytes of padding + 1 byte of padding length)
1294+
st.wantWindowUpdate(0, 11)
1295+
// Expect to immediately get our 5 bytes of padding back at stream level.
12921296
st.wantWindowUpdate(1, 5)
12931297

12941298
puppet.do(readBodyHandler(t, "abc"))
1295-
st.wantWindowUpdate(0, 3)
12961299
st.wantWindowUpdate(1, 3)
12971300

12981301
puppet.do(readBodyHandler(t, "def"))
1299-
st.wantWindowUpdate(0, 3)
13001302
st.wantWindowUpdate(1, 3)
13011303
}
13021304

0 commit comments

Comments
 (0)
Please sign in to comment.