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

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

proposal: io: add NewReaderAt interface #34305

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

Comments

@vincent-163
Copy link

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
@ianlancetaylor
Copy link
Contributor

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 (old) Dec 4, 2019
@rsc
Copy link
Contributor

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 (old) Dec 11, 2019
@rsc
Copy link
Contributor

rsc commented Jan 8, 2020

No change in consensus, so declining.

@rsc rsc closed this as completed Jan 8, 2020
@rsc rsc moved this from Likely Decline to Declined in Proposals (old) Jan 8, 2020
@golang golang locked and limited conversation to collaborators Jan 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

4 participants