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

net: no way to use sendfile with offset and count #9113

Closed
jbardin opened this issue Nov 17, 2014 · 5 comments
Closed

net: no way to use sendfile with offset and count #9113

jbardin opened this issue Nov 17, 2014 · 5 comments

Comments

@jbardin
Copy link
Contributor

jbardin commented Nov 17, 2014

A go TCP server can efficiently server files to clients using the ReadFrom method, but
if the protocol allows partial files, e.g. HTTP Range requests, we have to fallback to
copying all the data again for each connection.

Currently sendfile is only accessible by calling *TCPConn.ReadFrom(r io.Reader), with no
way to set the offset and count. Internally, sendfile uses to the netFD writelock and
file descriptor. Doing this outside of the net package is awkward and requires putting
the connection into blocking mode.

One option is to special case an io.SectionReader in the sendfile implementation, though
this requires modifying SectionReader in a way to expose the underlying io.Reader,
offset, and count fields.
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main, release-go1.5.

@rsc
Copy link
Contributor

rsc commented Nov 21, 2014

Comment 2:

There is a way.
f, _ := os.Open(filename)
f.Seek(offset, 0)
c.ReadFrom(io.LimitReader(f, count))

Status changed to Invalid.

@jbardin
Copy link
Contributor Author

jbardin commented Nov 21, 2014

Comment 3:

That doesn't currently work either, because an io.LimitedReader isn't an *io.File, and
fails the type assertion in sendfile_linux.go. It would be nice if that worked too.
Using Seek though still doesn't quite cover the sendfile api. If you call sendfile with
an offset, it does not modify the current file offset.

@jbardin
Copy link
Contributor Author

jbardin commented Nov 21, 2014

Comment 4:

(Sorry, I missed the re-assignment of r from the LimitedReader)
Though using Seek though still doesn't quite cover the sendfile api. If you call
sendfile with an offset, it does not modify the current file offset.

@rsc
Copy link
Contributor

rsc commented Nov 21, 2014

Comment 5:

We're not trying to cover the sendfile API. We're trying to make it possible to write
meaningful portable code that uses the sendfile optimization automatically. And we have.
ReadFrom changes the file offset. You can't avoid that and still have ReadFrom. So if
you really can't modify the file offset you're asking us to introduce a completely
different way to get to sendfile on top of what's already there. And I'd really rather
not. It's easy to open a file multiple times.

@bradfitz bradfitz modified the milestone: Go1.5 Dec 16, 2014
@mikioh mikioh removed this from the Go1.5 milestone Mar 10, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants