golang / snappy

The Snappy compression format in the Go programming language.
BSD 3-Clause "New" or "Revised" License
1.52k stars 164 forks source link

Implement Reader.Close #70

Closed danteu closed 1 year ago

danteu commented 1 year ago

This patch implements Reader.Close, which satisfies the io.Closer interface. Reader now, in turn, satisfies the io.ReadCloser interface.

cespare commented 1 year ago

Why is this needed?

danteu commented 1 year ago

Why is this needed?

By snappy.Reader satisfying the io.ReadCloser interface, it can now be used for http.Request.Body which allows handling snappy-encoded HTTP bodies. One particular use case I'm interested in is described in https://github.com/prometheus/pushgateway/issues/441

cespare commented 1 year ago

There's nothing to actually close, though, so I don't think it makes sense to implement Closer.

More subtly, if the underlying io.Reader is a ReadCloser, it's error-prone that the snappy Reader.Close doesn't close the underlying reader.

You can use io.NopCloser to wrap the snappy Reader in cases where you need to use it as a ReadCloser.

danteu commented 1 year ago

Makes sense, thanks for the quick reply.