Skip to content

Commit 8459cd9

Browse files
GODRIVER-3444 Remove error parameter from CalculateMaxTimeMS
1 parent 9d03793 commit 8459cd9

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

internal/driverutil/operation.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,27 @@ const (
3838
BulkWriteOp = "bulkWrite" // BulkWriteOp is the name for client-level bulk write
3939
)
4040

41-
func CalculateMaxTimeMS(ctx context.Context, rttMin time.Duration, rttStats string, err error) (int64, error) {
41+
func CalculateMaxTimeMS(ctx context.Context, rttMin time.Duration) (int64, bool) {
4242
deadline, ok := ctx.Deadline()
4343
if !ok {
44-
return 0, nil
44+
return 0, true
4545
}
4646

4747
remainingTimeout := time.Until(deadline)
48+
fmt.Println(remainingTimeout, rttMin)
4849

4950
// Always round up to the next millisecond value so we never truncate the calculated
5051
// maxTimeMS value (e.g. 400 microseconds evaluates to 1ms, not 0ms).
5152
maxTimeMS := int64((remainingTimeout - rttMin) / time.Millisecond)
52-
if maxTimeMS <= 0 {
53-
return 0, fmt.Errorf(
54-
"remaining time %v until context deadline is less than or equal to min network round-trip time %v (%v): %w",
55-
remainingTimeout,
56-
rttMin,
57-
rttStats,
58-
err)
59-
}
6053

6154
// The server will return a "BadValue" error if maxTimeMS is greater
6255
// than the maximum positive int32 value (about 24.9 days). If the
6356
// user specified a timeout value greater than that, omit maxTimeMS
6457
// and let the client-side timeout handle cancelling the op if the
6558
// timeout is ever reached.
6659
if maxTimeMS > math.MaxInt32 {
67-
return 0, nil
60+
return 0, false
6861
}
6962

70-
return maxTimeMS, nil
63+
return maxTimeMS, true
7164
}

x/mongo/driver/batch_cursor.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,14 @@ func (bc *BatchCursor) getMore(ctx context.Context) {
391391
if ctxDeadlineSet {
392392
rttMonitor := bc.Server().RTTMonitor()
393393

394-
var err error
395-
maxTimeMS, err = driverutil.CalculateMaxTimeMS(ctx, rttMonitor.Min(), rttMonitor.Stats(), ErrDeadlineWouldBeExceeded)
396-
if err != nil {
397-
return nil, err
394+
var ok bool
395+
maxTimeMS, ok = driverutil.CalculateMaxTimeMS(ctx, rttMonitor.Min())
396+
if !ok && maxTimeMS <= 0 {
397+
return nil, fmt.Errorf(
398+
"calculated server-side timeout (%v ms) is less than or equal to 0 (%v): %w",
399+
maxTimeMS,
400+
rttMonitor.Stats(),
401+
ErrDeadlineWouldBeExceeded)
398402
}
399403
}
400404

x/mongo/driver/operation.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ func (op Operation) Execute(ctx context.Context) error {
698698
}
699699

700700
// Calculate maxTimeMS value to potentially be appended to the wire message.
701-
maxTimeMS, err := op.calculateMaxTimeMS(ctx, srvr.RTTMonitor().Min(), srvr.RTTMonitor().Stats())
701+
maxTimeMS, err := op.calculateMaxTimeMS(ctx, srvr.RTTMonitor().Min())
702702
if err != nil {
703703
return err
704704
}
@@ -1719,12 +1719,22 @@ func (op Operation) addClusterTime(dst []byte, desc description.SelectedServer)
17191719
// if the ctx is a Timeout context. If the context is not a Timeout context, it uses the
17201720
// operation's MaxTimeMS if set. If no MaxTimeMS is set on the operation, and context is
17211721
// not a Timeout context, calculateMaxTimeMS returns 0.
1722-
func (op Operation) calculateMaxTimeMS(ctx context.Context, rttMin time.Duration, rttStats string) (int64, error) {
1722+
func (op Operation) calculateMaxTimeMS(ctx context.Context, rttMin time.Duration) (int64, error) {
17231723
if op.OmitMaxTimeMS {
17241724
return 0, nil
17251725
}
17261726

1727-
return driverutil.CalculateMaxTimeMS(ctx, rttMin, rttStats, ErrDeadlineWouldBeExceeded)
1727+
// Calculate maxTimeMS value to potentially be appended to the wire message.
1728+
maxTimeMS, ok := driverutil.CalculateMaxTimeMS(ctx, rttMin)
1729+
if !ok && maxTimeMS <= 0 {
1730+
return 0, fmt.Errorf(
1731+
"calculated server-side timeout (%v ms) is less than or equal to 0 (%v): %w",
1732+
maxTimeMS,
1733+
rttMin,
1734+
ErrDeadlineWouldBeExceeded)
1735+
}
1736+
1737+
return maxTimeMS, nil
17281738
}
17291739

17301740
// updateClusterTimes updates the cluster times for the session and cluster clock attached to this

x/mongo/driver/operation_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,14 @@ func TestOperation(t *testing.T) {
312312
t.Run(tc.name, func(t *testing.T) {
313313
t.Parallel()
314314

315-
got, err := tc.op.calculateMaxTimeMS(tc.ctx, tc.rttMin, tc.rttStats)
315+
got, err := tc.op.calculateMaxTimeMS(tc.ctx, tc.rttMin)
316316

317317
// Assert that the calculated maxTimeMS is less than or equal to the expected value. A few
318318
// milliseconds will have elapsed toward the context deadline, and (remainingTimeout
319319
// - rtt90) will be slightly smaller than the expected value.
320320
if got > tc.want {
321321
t.Errorf("maxTimeMS value higher than expected. got %v; wanted at most %v", got, tc.want)
322+
//t.Errorf("calculated server-side timeout (%v ms) is less than or equal to 0 (%v): %w",got, )
322323
}
323324
if !errors.Is(err, tc.err) {
324325
t.Errorf("error values do not match. got %v; want %v", err, tc.err)

0 commit comments

Comments
 (0)