From 236f36b9a27e6cd23e3f05cf02c3ba4b8e74a031 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Tue, 12 Nov 2024 16:21:50 -0800 Subject: [PATCH 1/2] fix: tcpreuse: remove windows specific code Unclear why, but this causes a lot of CPU usage according to @aschmahmann --- .../internal/sampledconn/sampledconn_other.go | 2 +- .../sampledconn/sampledconn_windows.go | 49 ------------------- 2 files changed, 1 insertion(+), 50 deletions(-) delete mode 100644 p2p/transport/tcpreuse/internal/sampledconn/sampledconn_windows.go diff --git a/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_other.go b/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_other.go index 7386112395..5197052fab 100644 --- a/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_other.go +++ b/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_other.go @@ -1,4 +1,4 @@ -//go:build !unix && !windows +//go:build !unix package sampledconn diff --git a/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_windows.go b/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_windows.go deleted file mode 100644 index 46b0617996..0000000000 --- a/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_windows.go +++ /dev/null @@ -1,49 +0,0 @@ -//go:build windows - -package sampledconn - -import ( - "errors" - "golang.org/x/sys/windows" - "syscall" -) - -func OSPeekConn(conn syscall.Conn) (PeekedBytes, error) { - s := PeekedBytes{} - - rawConn, err := conn.SyscallConn() - if err != nil { - return s, err - } - - readBytes := 0 - var readErr error - err = rawConn.Read(func(fd uintptr) bool { - for readBytes < peekSize { - var n uint32 - flags := uint32(windows.MSG_PEEK) - wsabuf := windows.WSABuf{ - Len: uint32(len(s) - readBytes), - Buf: &s[readBytes], - } - - readErr = windows.WSARecv(windows.Handle(fd), &wsabuf, 1, &n, &flags, nil, nil) - if errors.Is(readErr, windows.WSAEWOULDBLOCK) { - return false - } - if readErr != nil { - return true - } - readBytes += int(n) - } - return true - }) - if readErr != nil { - return s, readErr - } - if err != nil { - return s, err - } - - return s, nil -} From cfa8b3b9400cf7b5501c31c812ca440c528bc14f Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Thu, 14 Nov 2024 12:35:39 -0800 Subject: [PATCH 2/2] Use readfull --- p2p/transport/tcpreuse/internal/sampledconn/sampledconn_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_test.go b/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_test.go index a7c5a65f33..d5b31009e2 100644 --- a/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_test.go +++ b/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_test.go @@ -58,7 +58,7 @@ func TestSampledConn(t *testing.T) { assert.Equal(t, "hel", string(peeked[:])) buf := make([]byte, 5) - _, err = clientConn.Read(buf) + _, err = io.ReadFull(clientConn, buf) assert.NoError(t, err) assert.Equal(t, "hello", string(buf)) } else {