I have a situation where I want to take a stream of data from an io Reader and buffer it before sending it as a stream over http post, in the case of connection errors. For this I'm planning to use a bytes.Buffer and copy from the reader to the buffer. A separate goroutine would read from the buffer and pass the reader to a http.Request.
Now, the behavior from bytes.Buffer ReadFrom is not what I'm expecting. I'm simulating a data stream with a pipe:
r, w := io.Pipe()
go func() {
for i := 0; i < 10; i++ {
w.Write([]byte(fmt.Sprintf("foobar %d
", i)))
time.Sleep(200 * time.Millisecond)
}
w.Close()
}()
When reading from the reader into a byte slice, the Read returns after each write, however, when using ReadFrom, it doesn't stop reading until the writer is closed. This:
buf := &bytes.Buffer{}
nIn, err = buf.ReadFrom(r)
reads all ten lines before returning, while:
buf := make([]byte, 64)
nIn, err = r.Read(buf)
returns after each line. Why is the behavior different?