aboutsummaryrefslogtreecommitdiff
path: root/src/net/sendfile_test.go
diff options
context:
space:
mode:
authorDamien Neil <dneil@google.com>2024-10-25 11:47:53 -0700
committerDamien Neil <dneil@google.com>2024-10-28 23:45:31 +0000
commit98b3be702b05e8fc071fececf7bf44f078bf032f (patch)
tree6dd003824218c5120d94be622030f7cdddf07248 /src/net/sendfile_test.go
parentcd54b9bae94b36f67869ef174cbb432bc4012183 (diff)
downloadgo-98b3be702b05e8fc071fececf7bf44f078bf032f.tar.xz
os, net, internal/poll: combine unix sendfile implementations
The internal/poll/sendfile_{bsd,linux,solaris}.go implementations have more in common than not. Combine into a single sendfile_unix.go. The net and os packages have redundant code dealing with sendfile quirks on non-Linux Unix systems, such as the need to determine the size of the source file before sending. Move the common code into internal/poll. Remove some obsolete or incorrect behaviors: Drop the maximum sendfile chunk size. If we ask the kernel to copy more data than it is willing to send, it'll copy up to its limit. There was a comment in net/sendfile_unix_alt.go indicating that copying more bytes than a file contains results in the kernel looping back to the start of the file. I am unable to replicate this behavior anywhere. Dropped the comment, the workarounds, and added a test covering this case. Darwin, Dragonfly, and FreeBSD all support copying the entire contents of a file by passing 0 for the copy limit. Take advantage of this. Change-Id: I9f707ac7a27c165020ae02a6b5bb8f6f16f3c530 Reviewed-on: https://go-review.googlesource.com/c/go/+/621416 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/net/sendfile_test.go')
-rw-r--r--src/net/sendfile_test.go14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/net/sendfile_test.go b/src/net/sendfile_test.go
index 4f3411565b..64b33a54e2 100644
--- a/src/net/sendfile_test.go
+++ b/src/net/sendfile_test.go
@@ -69,7 +69,10 @@ func expectSendfile(t *testing.T, wantConn Conn, f func()) {
}
}
-func TestSendfile(t *testing.T) {
+func TestSendfile(t *testing.T) { testSendfile(t, 0) }
+func TestSendfileWithExactLimit(t *testing.T) { testSendfile(t, newtonLen) }
+func TestSendfileWithLimitLargerThanFile(t *testing.T) { testSendfile(t, newtonLen*2) }
+func testSendfile(t *testing.T, limit int64) {
ln := newLocalListener(t, "tcp")
defer ln.Close()
@@ -104,7 +107,14 @@ func TestSendfile(t *testing.T) {
sbytes, err = io.Copy(conn, f)
default:
expectSendfile(t, conn, func() {
- sbytes, err = io.Copy(conn, f)
+ if limit > 0 {
+ sbytes, err = io.CopyN(conn, f, limit)
+ if err == io.EOF && limit > newtonLen {
+ err = nil
+ }
+ } else {
+ sbytes, err = io.Copy(conn, f)
+ }
})
}
if err != nil {