Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: io: add NewReaderAt interface #34305

Open
vincent-163 opened this issue Sep 15, 2019 · 2 comments
Open

proposal: io: add NewReaderAt interface #34305

vincent-163 opened this issue Sep 15, 2019 · 2 comments

Comments

@vincent-163
Copy link

@vincent-163 vincent-163 commented Sep 15, 2019

I propose to add the following interface type to package io:

type NewReaderAt interface {
  NewReaderAt(size int64, off int64) io.Reader
}

The type name might need to be changed since it is the same as the method name, which can be confusing.

It is similar to the io.ReaderAt interface, but instead of writing to a buffer, it returns a new io.Reader. If there were any errors while creating the io.Reader, the error shall be returned in the Read method of io.Reader.

The major use case is when the underlying file is on a remote server, such as an HTTP server. A naive io.ReaderAt implementation have to make HTTP requests with the Range header for each ReadAt call, since it cannot predict how much bytes to read ahead. A user who wants to read a range of the file as a stream would either have to buffer the entire section of the file into memory using one ReadAt call, or make an HTTP request each time the buffer is drained, wasting time on round trips and HTTP overhead. Using the io.NewReaderAt interface, the user creates an io.Reader for the file section and then stream it, while the implementation needs to make only one HTTP request.

@gopherbot gopherbot added this to the Proposal milestone Sep 15, 2019
@gopherbot gopherbot added the Proposal label Sep 15, 2019
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Sep 16, 2019

I don't see any NewReaderAt methods in the standard library. It doesn't seem to make sense to add an interface type.

The discussion implies that you are suggesting that the HTTP server code should have a NewReaderAt method. Let's talk about that before talking adding an interface type.

@rsc rsc added this to Incoming in Proposals Dec 4, 2019
@rsc

This comment has been minimized.

Copy link
Contributor

@rsc rsc commented Dec 11, 2019

We have no NewReader, NewWriter, etc interfaces in the standard library, and as Ian said we also have no NewReaderAt methods.

Note that defining an interface only gives a name to something you can already write yourself. So not having the interface in the standard library can't be holding back code you want to write.

For these reasons, this seems like a likely decline.

Leaving open for a week for final comments.

@rsc rsc moved this from Incoming to Likely Decline in Proposals Dec 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Proposals
Likely Decline
4 participants
You can’t perform that action at this time.