Commit 29665f4
authored
transport: Pool read buffers used by the HTTP/2 framer (#9032)
## Problem
The HTTP/2 framer in gRPC uses a `bufio.Reader` with a 32KB buffer by
default. When there are a large number of transports, these buffers
consume significant memory, even when the transport is idle.
## Solution
#8964 added a `ReadyReader` interface that allows non-memory-pinning
reads. This PR replaces the standard `bufio.Reader` with a custom
`io.Reader` implementation that uses pooled buffers and releases the
buffer once all data is consumed.
To defer the re-allocation of the read buffer, the reader calls
`ReadOnReady` on the underlying `io.Reader`. For this to work, the
underlying `io.Reader` must implement either the `ReadyReader` interface
or `syscall.RawConn`. If neither condition is met, the framer gracefully
falls back to using the regular `bufio.Reader`.
Additional Changes:
* The ALTS connection has been refactored to implement the `ReadyReader`
interface.
* The write buffer pools used by the framer are updated to use the
`mem.BufferPool` interface, allowing the pools to be shared across both
read and write operations.
* Use `syscall.Read` instead of `unix.Read` to avoid triggering the race
detector, see comment for details.
* Add environment variable protection for the changes to allow fast
rollback.
## Benchmarks
In a [real-world
benchmark](https://github.com/arjan-bal/custom-go-client-benchmark/tree/retry-dp),
where a GCS directpath client downloads a file in a loop, the average
"in use" memory falls from 28.3MB to 21.3MB (-24%).
Local Benchmarks show no significant difference
```
❯ go run benchmark/benchresult/main.go streaming-before streaming-after
streaming-networkMode_Local-bufConn_false-keepalive_false-benchTime_2m0s-trace_false-latency_0s-kbps_0-MTU_0-maxConcurrentCa
lls_120-reqSize_1024B-respSize_1024B-compressor_off-channelz_false-preloader_false-clientReadBufferSize_-1-clientWriteBuffer
Size_-1-serverReadBufferSize_-1-serverWriteBufferSize_-1-sleepBetweenRPCs_0s-connections_1-recvBufferPool_simple-sharedWrite
Buffer_true
Title Before After Percentage
TotalOps 29981273 29966908 -0.05%
SendOps 0 0 NaN%
RecvOps 0 0 NaN%
Bytes/op 4971.06 4971.41 0.00%
Allocs/op 19.79 19.79 0.00%
ReqT/op 2046721570.13 2045740919.47 -0.05%
RespT/op 2046721570.13 2045740919.47 -0.05%
50th-Lat 461.523µs 460.906µs -0.13%
90th-Lat 654.435µs 655.327µs 0.14%
99th-Lat 1.225856ms 1.240984ms 1.23%
Avg-Lat 478.845µs 479.553µs 0.15%
GoVersion go1.25.0 go1.25.0
GrpcVersion 1.81.0-dev 1.81.0-dev
```
RELEASE NOTES:
* transport: Pool HTTP/2 framer read buffers to reduce idle memory
consumption. Currently limited to Linux for ALTS and non-encrypted
transports (TCP, Unix). To disable, set
`GRPC_GO_EXPERIMENTAL_HTTP_FRAMER_READ_BUFFER_POOLING=false` and report
any issues.1 parent f6304e9 commit 29665f4
7 files changed
Lines changed: 175 additions & 51 deletions
File tree
- credentials/alts/internal/conn
- internal
- envconfig
- transport
- readyreader
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
78 | | - | |
| 79 | + | |
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
83 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
84 | 88 | | |
85 | 89 | | |
86 | 90 | | |
87 | | - | |
| 91 | + | |
88 | 92 | | |
89 | 93 | | |
90 | 94 | | |
| |||
126 | 130 | | |
127 | 131 | | |
128 | 132 | | |
129 | | - | |
| 133 | + | |
| 134 | + | |
130 | 135 | | |
131 | 136 | | |
132 | 137 | | |
| |||
163 | 168 | | |
164 | 169 | | |
165 | 170 | | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
166 | 181 | | |
167 | 182 | | |
168 | 183 | | |
169 | 184 | | |
170 | 185 | | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
171 | 192 | | |
172 | 193 | | |
173 | 194 | | |
174 | 195 | | |
175 | 196 | | |
176 | 197 | | |
177 | 198 | | |
| 199 | + | |
178 | 200 | | |
179 | 201 | | |
180 | | - | |
| 202 | + | |
181 | 203 | | |
182 | 204 | | |
183 | 205 | | |
| |||
217 | 239 | | |
218 | 240 | | |
219 | 241 | | |
220 | | - | |
| 242 | + | |
221 | 243 | | |
222 | 244 | | |
223 | 245 | | |
224 | 246 | | |
225 | 247 | | |
226 | 248 | | |
227 | | - | |
| 249 | + | |
228 | 250 | | |
229 | 251 | | |
230 | 252 | | |
231 | 253 | | |
232 | 254 | | |
233 | | - | |
| 255 | + | |
234 | 256 | | |
235 | 257 | | |
236 | 258 | | |
237 | 259 | | |
238 | 260 | | |
239 | 261 | | |
240 | | - | |
| 262 | + | |
241 | 263 | | |
242 | 264 | | |
243 | 265 | | |
244 | 266 | | |
245 | 267 | | |
246 | 268 | | |
247 | | - | |
248 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
249 | 272 | | |
250 | | - | |
| 273 | + | |
| 274 | + | |
251 | 275 | | |
252 | 276 | | |
253 | | - | |
| 277 | + | |
254 | 278 | | |
255 | 279 | | |
256 | 280 | | |
| |||
261 | 285 | | |
262 | 286 | | |
263 | 287 | | |
264 | | - | |
| 288 | + | |
265 | 289 | | |
266 | 290 | | |
267 | 291 | | |
268 | | - | |
| 292 | + | |
| 293 | + | |
269 | 294 | | |
270 | 295 | | |
271 | | - | |
| 296 | + | |
272 | 297 | | |
273 | 298 | | |
274 | 299 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
145 | 155 | | |
146 | 156 | | |
147 | 157 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
| |||
296 | 299 | | |
297 | 300 | | |
298 | 301 | | |
299 | | - | |
| 302 | + | |
300 | 303 | | |
301 | 304 | | |
302 | 305 | | |
303 | 306 | | |
304 | 307 | | |
305 | 308 | | |
306 | 309 | | |
307 | | - | |
| 310 | + | |
308 | 311 | | |
309 | 312 | | |
310 | 313 | | |
| |||
326 | 329 | | |
327 | 330 | | |
328 | 331 | | |
329 | | - | |
| 332 | + | |
330 | 333 | | |
331 | 334 | | |
332 | 335 | | |
| |||
407 | 410 | | |
408 | 411 | | |
409 | 412 | | |
410 | | - | |
411 | | - | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
412 | 428 | | |
413 | 429 | | |
414 | 430 | | |
415 | 431 | | |
416 | 432 | | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
| 433 | + | |
| 434 | + | |
422 | 435 | | |
423 | | - | |
| 436 | + | |
424 | 437 | | |
425 | | - | |
| 438 | + | |
426 | 439 | | |
427 | 440 | | |
428 | 441 | | |
| |||
578 | 591 | | |
579 | 592 | | |
580 | 593 | | |
581 | | - | |
582 | | - | |
583 | | - | |
584 | | - | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
585 | 598 | | |
586 | 599 | | |
587 | 600 | | |
588 | | - | |
589 | | - | |
590 | | - | |
591 | | - | |
592 | | - | |
593 | | - | |
594 | | - | |
| 601 | + | |
| 602 | + | |
595 | 603 | | |
596 | 604 | | |
597 | 605 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
34 | 38 | | |
35 | 39 | | |
36 | 40 | | |
| |||
259 | 263 | | |
260 | 264 | | |
261 | 265 | | |
262 | | - | |
| 266 | + | |
263 | 267 | | |
264 | 268 | | |
265 | 269 | | |
| |||
413 | 417 | | |
414 | 418 | | |
415 | 419 | | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
0 commit comments