-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
(*bufio.Writer).ReadFrom will use the underlying writer's ReadFrom method if present, but only when the bufio.Writer has no data buffered.
When the http package writes a request, it first writes the headers to a bufio.Writer and then uses io.Copy to write the request body. The buffered headers prevent the use of the underlying net.Conn's ReadFrom. Flushing the headers before the body may result in an unnecessary additional write, and possibly split a small request across multiple packets.
A similar problem may occur in other cases where a bufio.Writer has some amount of data written to it, followed by a large copy.
ReadFrom should instead fill out the current buffer, flush it, and then continue the operation with the underlying writer's ReadFrom (when present).
// ReadFrom implements io.ReaderFrom. This calls the underlying writer's ReadFrom method,
// if any. If b has buffered data, this fills the buffer before calling the underlying `ReadFrom`.
func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) {
...
}