Skip to content

Commit 2d988ed

Browse files
committed
improvements
1 parent b565a23 commit 2d988ed

File tree

3 files changed

+175
-183
lines changed

3 files changed

+175
-183
lines changed

x/mongo/driver/topology/connection.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package topology
99
import (
1010
"context"
1111
"crypto/tls"
12+
"encoding/binary"
1213
"errors"
1314
"fmt"
1415
"io"
@@ -472,10 +473,7 @@ func (c *connection) readWireMessage(ctx context.Context) ([]byte, error) {
472473

473474
func (c *connection) parseWmSizeBytes(wmSizeBytes [4]byte) (int32, error) {
474475
// read the length as an int32
475-
size := (int32(wmSizeBytes[0])) |
476-
(int32(wmSizeBytes[1]) << 8) |
477-
(int32(wmSizeBytes[2]) << 16) |
478-
(int32(wmSizeBytes[3]) << 24)
476+
size := int32(binary.LittleEndian.Uint32(wmSizeBytes[:]))
479477

480478
if size < 4 {
481479
return 0, fmt.Errorf("malformed message length: %d", size)
@@ -506,7 +504,7 @@ func (c *connection) read(ctx context.Context) (bytesRead []byte, errMsg string,
506504
}
507505
}()
508506

509-
needToWait := func(err error) bool {
507+
isCSOTTimeout := func(err error) bool {
510508
// If the error was a timeout error and CSOT is enabled, instead of
511509
// closing the connection mark it as awaiting response so the pool
512510
// can read the response before making it available to other
@@ -524,7 +522,7 @@ func (c *connection) read(ctx context.Context) (bytesRead []byte, errMsg string,
524522
// reading messages from an exhaust cursor.
525523
n, err := io.ReadFull(c.nc, sizeBuf[:])
526524
if err != nil {
527-
if l := int32(n); l == 0 && needToWait(err) {
525+
if l := int32(n); l == 0 && isCSOTTimeout(err) {
528526
c.awaitRemainingBytes = &l
529527
}
530528
return nil, "incomplete read of message header", err
@@ -540,7 +538,7 @@ func (c *connection) read(ctx context.Context) (bytesRead []byte, errMsg string,
540538
n, err = io.ReadFull(c.nc, dst[4:])
541539
if err != nil {
542540
remainingBytes := size - 4 - int32(n)
543-
if remainingBytes > 0 && needToWait(err) {
541+
if remainingBytes > 0 && isCSOTTimeout(err) {
544542
c.awaitRemainingBytes = &remainingBytes
545543
}
546544
return dst, "incomplete read of full message", err

x/mongo/driver/topology/pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ func bgRead(pool *pool, conn *connection, size int32) {
840840
}
841841
_, err = io.CopyN(io.Discard, conn.nc, int64(size))
842842
if err != nil {
843-
err = fmt.Errorf("error reading message of %d: %w", size, err)
843+
err = fmt.Errorf("error discarding %d byte message: %w", size, err)
844844
}
845845
}
846846

0 commit comments

Comments
 (0)