Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net/http: refactor ResponseWriter.ReadFrom to permit splice on Linux
Issue golang#40888 describes my motivation for this patch: . Rather than probe and guess if sendfile will work, fix the underlying issue of starting to respond before src is readable . Do not send a status OK if header has not yet been written and reading from src is destined to fail . Implicitly takes care of needsSniff() case This allows splice to work on linux when src is a socket or other spliceable non regular file. The extra read of 512 bytes may raise an objection, and that's fair, but we're already swapping some syscall prep work for another and a read of 512 probably will not impact the overall performance. For shorter bodies, there's likely less setup time. A little initial slop is not that unusual in zero copy network code, and sometimes actually helps. review feedback: use copyBufPool in both cases Use the same pool for the normal copy case as we do for the short read. Clean up and clarify comments. clean up code addressing feedback pass sendfile test the sendfile path will require >512 bytes to kick in, so send a 1024 byte file for this test. obey copy semantics pattern the initial read and write after io.Copy(), paying attention to either side's error and writing what has been read regardless. This could be done with an io.LimitedReader with io.Copy() except that returns the total bytes written, while a ReaderFrom needs to return the total bytes read. While we could adjust for this by looking at the limited reader's state, being explicit this way is probably more clear. create sendfile test data in temp dir Remove accidentally added 1024k file which was meant to be a 1024b file. Generate the test data in a temporary directory regardless.
- Loading branch information