Is this a Bufio bug?

I’m thinking about filing a ticket against bufio.ReadFrom()'s behavior of transparently passing the reader to the wrapped writer if it also implements ReadFrom. As this completely skips buffering, which is the whole point of using a bufio wrapper.

This came up in an application using go’s SFTP library that had many small writes to a remote file. It used bufio to group up the writes which greatly increased network throughput. When the SFTP library added ReadFrom() to it’s file abstraction this buffering stopped as it just skips the buffering and the network throughput tanked.

I understand why you’d want to pass it through like they do as it optimizes out the copies, but then what do you use when you want to buffer writes to an underlying object that supports ReadFrom?


The ticket from the SFTP library:

I’m not sure whether it’s a bug or not, but you could filter out the ReadFrom on writer side to work around it.

var f *sftp.File = ... // your writer

type writerOnly struct { io.Writer }
bw := bufio.NewWriter(writerOnly{f}) // no ReadFrom()

Thanks for the reply. I had thought of the same workaround, but it seems odd to have to introduce a shim like this to get a package to do what it advertises. That’s why I was thinking it might be a bug, in the documentation if nothing else.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.