@@ -8,7 +8,6 @@ package driverutil
88
99import (
1010 "context"
11- "fmt"
1211 "math"
1312 "time"
1413)
@@ -38,26 +37,32 @@ const (
3837 BulkWriteOp = "bulkWrite" // BulkWriteOp is the name for client-level bulk write
3938)
4039
40+ // CalculateMaxTimeMS calculates the maxTimeMS value to send to the server
41+ // based on the context deadline and the minimum round trip time. If the
42+ // calculated maxTimeMS is likely to cause a socket timeout, then this function
43+ // will return 0 and false.
4144func CalculateMaxTimeMS (ctx context.Context , rttMin time.Duration ) (int64 , bool ) {
4245 deadline , ok := ctx .Deadline ()
4346 if ! ok {
4447 return 0 , true
4548 }
4649
4750 remainingTimeout := time .Until (deadline )
48- fmt .Println (remainingTimeout , rttMin )
4951
5052 // Always round up to the next millisecond value so we never truncate the calculated
5153 // maxTimeMS value (e.g. 400 microseconds evaluates to 1ms, not 0ms).
5254 maxTimeMS := int64 ((remainingTimeout - rttMin ) / time .Millisecond )
55+ if maxTimeMS <= 0 {
56+ return 0 , false
57+ }
5358
5459 // The server will return a "BadValue" error if maxTimeMS is greater
5560 // than the maximum positive int32 value (about 24.9 days). If the
5661 // user specified a timeout value greater than that, omit maxTimeMS
5762 // and let the client-side timeout handle cancelling the op if the
5863 // timeout is ever reached.
5964 if maxTimeMS > math .MaxInt32 {
60- return 0 , false
65+ return 0 , true
6166 }
6267
6368 return maxTimeMS , true
0 commit comments