Skip to content

Commit

Permalink
Use remaining time if available
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez committed Dec 3, 2024
1 parent 14ca5ae commit 1f442bf
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions x/mongo/driver/topology/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package topology

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -857,7 +858,15 @@ func awaitPendingRead(ctx context.Context, pool *pool, conn *connection) error {

dl, contextDeadlineUsed := ctx.Deadline()
if !contextDeadlineUsed {
dl = time.Now().Add(PendingReadTimeout)
// If there is a remainingTime, use that. If not, use the static
// PendingReadTimeout. This is required since a user could provide a timeout
// for the first try that does not exceed the pending read timeout, fail,
// and then not use a timeout for a subsequent try.
if conn.remainingTime != nil {
dl = time.Now().Add(*conn.remainingTime)
} else {
dl = time.Now().Add(PendingReadTimeout)
}
}

err := conn.nc.SetReadDeadline(dl)
Expand All @@ -871,7 +880,7 @@ func awaitPendingRead(ctx context.Context, pool *pool, conn *connection) error {

st := time.Now()

if size == 0 {
if size == 0 { // Question: Would this alawys equal to zero?
var sizeBuf [4]byte
if _, err := io.ReadFull(conn.nc, sizeBuf[:]); err != nil {
conn.remainingTime = ptrutil.Ptr(*conn.remainingTime - time.Since(st))
Expand All @@ -891,7 +900,10 @@ func awaitPendingRead(ctx context.Context, pool *pool, conn *connection) error {
}
size -= 4
}
n, err := io.CopyN(io.Discard, conn.nc, int64(size))

buf := bytes.NewBuffer(nil)
n, err := io.CopyN(buf, conn.nc, int64(size))
fmt.Println("buf: ", buf)
if err != nil {
// If the read times out, record the bytes left to read before exiting.
nerr := net.Error(nil)
Expand Down

0 comments on commit 1f442bf

Please sign in to comment.