Commit 098e02d
feat(tcp): receive-buffer Dynamic Right-Sizing (autotune step 4 / R2-R4)
Fourth phase of the Tier-3 buffer auto-tuning plan
(docs/refactor/tcp_buffer_autotuning.md §4 R2-R4 / §10 step 4): grow the
advertised receive window toward the bandwidth-delay product, mirroring
Linux tcp_rcv_space_adjust. Builds on the R1 receiver-RTT estimator.
R2 (state):
- New RcvSpaceState (state/tcp__state__rcv_space.py): space (high-water
per-RTT copied bytes) + copied_anchor + time_ms — the measurement
window. Seeded self._rcv_space with space = initial rcv_wnd_max in
TcpSession.__init__, plus self._rcv_copied_total bumped by byte_count
in receive() so the policy can compute per-RTT throughput.
R3 + R4 (trigger + policy):
- TcpSession._maybe_adjust_rcv_space() runs from receive() after the
drain (the app-thread analogue of Linux calling it from tcp_recvmsg).
Once per receiver-RTT, when copied exceeds the last measurement, it
computes rcvwin = 2*copied + 16*advmss plus a sender-rate headroom
term, clamps at min(tcp.rmem.max, 0xFFFF << rcv_wsc) — the R0 WSCALE
ceiling so the grown window stays expressible under the negotiated
shift — and calls the existing grow-only grow_rcv_wnd_max. per_mss is
the bare advmss (no tcp_adv_win_scale overhead — the locked Tier-1
deviation), so window == buffer size.
- No-op until the receiver RTT exists, before one RTT elapsed, when
tcp.moderate_rcvbuf is off, or when SO_RCVBUF is set (SOCK_RCVBUF_LOCK
— makes DRS and the A3 mid-connection resize mutually-exclusive
writers of rcv_wnd_max, preserving the single-writer invariant).
DRS is now functionally complete. It stays inert on the common path
until per-RTT throughput exceeds the start window; no behaviour change
for connections that never fill it or that pin SO_RCVBUF.
Tests-first: test__tcp__session__drs.py (7 cases — grow toward BDP,
cadence gate, no-RTT skip, SO_RCVBUF lock, moderate_rcvbuf off,
rmem.max clamp, receive()-path trigger). RED pre-impl: AttributeError
(_rcv_space absent). Two grow tests initially failed because the default
handshake negotiates no WSCALE, so the R0 ceiling (0xFFFF << 0) correctly
capped at 65535 — the RED step surfaced that the grow tests must
negotiate WSCALE; fixed by establishing with peer_wscale=7. Plan §4 /
§10 step 4 marked done.
Reference: RFC 9293 §3.8.6 (receive window management).
Reference: RFC 7323 §2 (window scale bounds the grown window).
Reference: Linux tcp_rcv_space_adjust / net.ipv4.tcp_moderate_rcvbuf.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent cc74c38 commit 098e02d
4 files changed
Lines changed: 454 additions & 1 deletion
File tree
- docs/refactor
- packages/pytcp/pytcp
- protocols/tcp
- session
- state
- tests/integration/protocols/tcp
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
209 | 209 | | |
210 | 210 | | |
211 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
212 | 229 | | |
213 | 230 | | |
214 | 231 | | |
| |||
513 | 530 | | |
514 | 531 | | |
515 | 532 | | |
516 | | - | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
517 | 540 | | |
518 | 541 | | |
519 | 542 | | |
| |||
Lines changed: 64 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| |||
344 | 345 | | |
345 | 346 | | |
346 | 347 | | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
347 | 355 | | |
348 | 356 | | |
349 | 357 | | |
| |||
1304 | 1312 | | |
1305 | 1313 | | |
1306 | 1314 | | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
1307 | 1318 | | |
1308 | 1319 | | |
1309 | 1320 | | |
| |||
1325 | 1336 | | |
1326 | 1337 | | |
1327 | 1338 | | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
1328 | 1346 | | |
1329 | 1347 | | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
1330 | 1394 | | |
1331 | 1395 | | |
1332 | 1396 | | |
| |||
Lines changed: 63 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
0 commit comments