Read reads n bytes from the wire for this stream.
(n int)
| 395 | |
| 396 | // Read reads n bytes from the wire for this stream. |
| 397 | func (s *Stream) read(n int) (data mem.BufferSlice, err error) { |
| 398 | // Don't request a read if there was an error earlier |
| 399 | if er := s.trReader.er; er != nil { |
| 400 | return nil, er |
| 401 | } |
| 402 | // gRPC Go accepts data frames with a maximum length of 16KB. Larger |
| 403 | // messages must be split into multiple frames. We pre-allocate the |
| 404 | // buffer to avoid resizing during the read loop, but cap the initial |
| 405 | // capacity to 128 frames (2MB) to prevent over-allocation or panics |
| 406 | // when reading extremely large streams. |
| 407 | allocCap := min(ceil(n, http2MaxFrameLen), 128) |
| 408 | data = make(mem.BufferSlice, 0, allocCap) |
| 409 | s.readRequester.requestRead(n) |
| 410 | for n != 0 { |
| 411 | buf, err := s.trReader.Read(n) |
| 412 | var bufLen int |
| 413 | if buf != nil { |
| 414 | bufLen = buf.Len() |
| 415 | } |
| 416 | n -= bufLen |
| 417 | if n == 0 { |
| 418 | err = nil |
| 419 | } |
| 420 | if err != nil { |
| 421 | if bufLen > 0 && err == io.EOF { |
| 422 | err = io.ErrUnexpectedEOF |
| 423 | } |
| 424 | data.Free() |
| 425 | return nil, err |
| 426 | } |
| 427 | data = append(data, buf) |
| 428 | } |
| 429 | return data, nil |
| 430 | } |
| 431 | |
| 432 | // noCopy may be embedded into structs which must not be copied |
| 433 | // after the first use. |