@@ -379,31 +379,28 @@ func (bc *BatchCursor) getMore(ctx context.Context) {
379
379
return
380
380
}
381
381
382
- dl , ok := ctx .Deadline ()
383
- fmt .Println ("bc deadline: " , time .Until (dl ), ok )
384
- fmt .Println ("bc maxAwaitTime: " , * bc .maxAwaitTime )
385
- if bc .maxAwaitTime != nil {
386
- }
387
-
388
382
bc .err = Operation {
389
383
CommandFn : func (dst []byte , _ description.SelectedServer ) ([]byte , error ) {
390
- conn , err := bc .Server ().Connection (context .Background ())
391
- if err != nil {
392
- panic (err )
393
- }
394
-
395
- rttMonitor := bc .Server ().RTTMonitor ()
396
- maxTimeMS , err := driverutil .CalculateMaxTimeMS (ctx , rttMonitor .Min (), rttMonitor .Stats (), ErrDeadlineWouldBeExceeded )
397
- if bc .maxAwaitTime != nil && int64 (* bc .maxAwaitTime / time .Millisecond ) >= maxTimeMS {
398
- fmt .Println (int64 (* bc .maxAwaitTime / time .Millisecond ), maxTimeMS )
399
- return nil , ErrDeadlineWouldBeExceeded
400
- }
401
-
402
- dl , ok := ctx .Deadline ()
403
- fmt .Println ("bc deadline: " , time .Until (dl ), ok )
404
- fmt .Println ("min (bc): " , bc .Server ().RTTMonitor ().Min (), conn .Describer .ID (), conn .Describer .Address (), maxTimeMS , time .Until (dl ), ok , bc .maxAwaitTime )
405
- if err != nil {
406
- return nil , err
384
+ // If maxAwaitTime > remaining timeoutMS - minRoundTripTime, then use
385
+ // send remaining TimeoutMS - minRoundTripTime allowing the server an
386
+ // opportunity to respond with an empty batch.
387
+ var maxTimeMS int64
388
+ if bc .maxAwaitTime != nil {
389
+ _ , ctxDeadlineSet := ctx .Deadline ()
390
+
391
+ if ctxDeadlineSet {
392
+ rttMonitor := bc .Server ().RTTMonitor ()
393
+
394
+ var err error
395
+ maxTimeMS , err = driverutil .CalculateMaxTimeMS (ctx , rttMonitor .Min (), rttMonitor .Stats (), ErrDeadlineWouldBeExceeded )
396
+ if err != nil {
397
+ return nil , err
398
+ }
399
+ }
400
+
401
+ if ! ctxDeadlineSet || bc .maxAwaitTime .Milliseconds () < maxTimeMS {
402
+ maxTimeMS = bc .maxAwaitTime .Milliseconds ()
403
+ }
407
404
}
408
405
409
406
dst = bsoncore .AppendInt64Element (dst , "getMore" , bc .id )
@@ -412,10 +409,8 @@ func (bc *BatchCursor) getMore(ctx context.Context) {
412
409
dst = bsoncore .AppendInt32Element (dst , "batchSize" , numToReturn )
413
410
}
414
411
415
- if bc . maxAwaitTime != nil && * bc . maxAwaitTime > 0 {
412
+ if maxTimeMS > 0 {
416
413
dst = bsoncore .AppendInt64Element (dst , "maxTimeMS" , maxTimeMS )
417
-
418
- //dst = bsoncore.AppendInt64Element(dst, "maxTimeMS", int64(*bc.maxAwaitTime)/int64(time.Millisecond))
419
414
}
420
415
421
416
comment , err := codecutil .MarshalValue (bc .comment , bc .encoderFn )
0 commit comments