diff options
| author | Shibi J M <shibisjm@gmail.com> | 2025-05-20 03:59:15 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2025-05-20 11:06:59 -0700 |
| commit | be0cc937ec9c109da90ec4d7da5af89606f8c0cf (patch) | |
| tree | 7081272ffb6c8b02282455e8072df8514a621749 /src/net | |
| parent | 0c7311e9ca8440801b40928878db623f98e3008f (diff) | |
| download | go-be0cc937ec9c109da90ec4d7da5af89606f8c0cf.tar.xz | |
net: avoid using Windows' TransmitFile on non-server machines
Windows API's TransmitFile function is limited to two concurrent
operations on workstation and client versions of Windows. This change
modifies the net.sendFile function to perform no work in such cases
so that TransmitFile is avoided.
Fixes #73746
Change-Id: Iba70d5d2758bf986e80c78254c8e9e10b39bb368
GitHub-Last-Rev: 315ddc0cd8034f52632dc31baf35057a8bad9bcd
GitHub-Pull-Request: golang/go#73758
Reviewed-on: https://go-review.googlesource.com/c/go/+/673855
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Auto-Submit: Damien Neil <dneil@google.com>
Reviewed-by: Quim Muntal <quimmuntal@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/net')
| -rw-r--r-- | src/net/sendfile.go | 5 | ||||
| -rw-r--r-- | src/net/sendfile_nonwindows.go | 12 | ||||
| -rw-r--r-- | src/net/sendfile_stub.go | 4 | ||||
| -rw-r--r-- | src/net/sendfile_test.go | 4 | ||||
| -rw-r--r-- | src/net/sendfile_windows.go | 16 |
5 files changed, 36 insertions, 5 deletions
diff --git a/src/net/sendfile.go b/src/net/sendfile.go index 0a41241561..0e0fcc40ff 100644 --- a/src/net/sendfile.go +++ b/src/net/sendfile.go @@ -12,8 +12,6 @@ import ( "syscall" ) -const supportsSendfile = true - // sendFile copies the contents of r to c using the sendfile // system call to minimize copies. // @@ -22,6 +20,9 @@ const supportsSendfile = true // // if handled == false, sendFile performed no work. func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) { + if !supportsSendfile() { + return 0, nil, false + } var remain int64 = 0 // 0 writes the entire file lr, ok := r.(*io.LimitedReader) if ok { diff --git a/src/net/sendfile_nonwindows.go b/src/net/sendfile_nonwindows.go new file mode 100644 index 0000000000..2106d37895 --- /dev/null +++ b/src/net/sendfile_nonwindows.go @@ -0,0 +1,12 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build linux || (darwin && !ios) || dragonfly || freebsd || solaris + +package net + +// Always true except for workstation and client versions of Windows +func supportsSendfile() bool { + return true +} diff --git a/src/net/sendfile_stub.go b/src/net/sendfile_stub.go index 7f31cc63e1..17d8d5448f 100644 --- a/src/net/sendfile_stub.go +++ b/src/net/sendfile_stub.go @@ -8,7 +8,9 @@ package net import "io" -const supportsSendfile = false +func supportsSendfile() bool { + return false +} func sendFile(c *netFD, r io.Reader) (n int64, err error, handled bool) { return 0, nil, false diff --git a/src/net/sendfile_test.go b/src/net/sendfile_test.go index b5039ff1d1..437d181508 100644 --- a/src/net/sendfile_test.go +++ b/src/net/sendfile_test.go @@ -31,11 +31,11 @@ const ( // expectSendfile runs f, and verifies that internal/poll.SendFile successfully handles // a write to wantConn during f's execution. // -// On platforms where supportsSendfile is false, expectSendfile runs f but does not +// On platforms where supportsSendfile() is false, expectSendfile runs f but does not // expect a call to SendFile. func expectSendfile(t *testing.T, wantConn Conn, f func()) { t.Helper() - if !supportsSendfile { + if !supportsSendfile() { f() return } diff --git a/src/net/sendfile_windows.go b/src/net/sendfile_windows.go new file mode 100644 index 0000000000..44ddb421a1 --- /dev/null +++ b/src/net/sendfile_windows.go @@ -0,0 +1,16 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package net + +import "internal/syscall/windows" + +// Workstation and client versions of Windows limit the number +// of concurrent TransmitFile operations allowed on the system +// to a maximum of two. Please see: +// https://learn.microsoft.com/en-us/windows/win32/api/mswsock/nf-mswsock-transmitfile +// https://golang.org/issue/73746 +func supportsSendfile() bool { + return windows.SupportUnlimitedTransmitFile() +} |
