From 6bc327b3cee8bb98f2c6142f82d48ebaafa92028 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Tue, 31 Jan 2023 20:25:57 -0600 Subject: [PATCH] Find fastest possible read time for fakeNonblockingReadWaitDuration The first 5 fake non-blocking reads are limited to 1 byte. This should ensure that there is a measurement of a read where bytes are already waiting in Go or the OS's read buffer. --- internal/nbconn/nbconn.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/nbconn/nbconn.go b/internal/nbconn/nbconn.go index 9e014aae..6a97511a 100644 --- a/internal/nbconn/nbconn.go +++ b/internal/nbconn/nbconn.go @@ -91,6 +91,7 @@ type NetConn struct { readDeadlineLock sync.Mutex readDeadline time.Time readNonblocking bool + fakeNonBlockingShortReadCount int fakeNonblockingReadWaitDuration time.Duration writeDeadlineLock sync.Mutex @@ -416,6 +417,13 @@ func (c *NetConn) fakeNonblockingRead(b []byte) (n int, err error) { c.readDeadlineLock.Lock() defer c.readDeadlineLock.Unlock() + // The first 5 reads only read 1 byte at a time. This should give us 4 chances to read when we are sure the bytes are + // already in Go or the OS's receive buffer. + if c.fakeNonBlockingShortReadCount < 5 && len(b) > 0 { + b = b[:1] + c.fakeNonBlockingShortReadCount++ + } + startTime := time.Now() deadline := startTime.Add(c.fakeNonblockingReadWaitDuration) if c.readDeadline.IsZero() || deadline.Before(c.readDeadline) {