Skip to content

Commit 2b69f03

Browse files
prestonvasquezqingyang-hu
authored andcommitted
GODRIVER-2209 Omit readPreference on BatchCursor commands (#1346)
1 parent 564d5ef commit 2b69f03

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

x/mongo/driver/batch_cursor.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ func (bc *BatchCursor) KillCursor(ctx context.Context) error {
305305
Legacy: LegacyKillCursors,
306306
CommandMonitor: bc.cmdMonitor,
307307
ServerAPI: bc.serverAPI,
308+
309+
// No read preference is passed to the killCursor command,
310+
// resulting in the default read preference: "primaryPreferred".
311+
// Since this could be confusing, and there is no requirement
312+
// to use a read preference here, we omit it.
313+
omitReadPreference: true,
308314
}.Execute(ctx)
309315
}
310316

@@ -398,6 +404,12 @@ func (bc *BatchCursor) getMore(ctx context.Context) {
398404
CommandMonitor: bc.cmdMonitor,
399405
Crypt: bc.crypt,
400406
ServerAPI: bc.serverAPI,
407+
408+
// No read preference is passed to the getMore command,
409+
// resulting in the default read preference: "primaryPreferred".
410+
// Since this could be confusing, and there is no requirement
411+
// to use a read preference here, we omit it.
412+
omitReadPreference: true,
401413
}.Execute(ctx)
402414

403415
// Once the cursor has been drained, we can unpin the connection if one is currently pinned.

x/mongo/driver/operation.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ type Operation struct {
308308

309309
// cmdName is only set when serializing OP_MSG and is used internally in readWireMessage.
310310
cmdName string
311+
312+
// omitReadPreference is a boolean that indicates whether to omit the
313+
// read preference from the command. This omition includes the case
314+
// where a default read preference is used when the operation
315+
// ReadPreference is not specified.
316+
omitReadPreference bool
311317
}
312318

313319
// shouldEncrypt returns true if this operation should automatically be encrypted.
@@ -1515,6 +1521,10 @@ func (op Operation) getReadPrefBasedOnTransaction() (*readpref.ReadPref, error)
15151521
}
15161522

15171523
func (op Operation) createReadPref(desc description.SelectedServer, isOpQuery bool) (bsoncore.Document, error) {
1524+
if op.omitReadPreference {
1525+
return nil, nil
1526+
}
1527+
15181528
// TODO(GODRIVER-2231): Instead of checking if isOutputAggregate and desc.Server.WireVersion.Max < 13, somehow check
15191529
// TODO if supplied readPreference was "overwritten" with primary in description.selectForReplicaSet.
15201530
if desc.Server.Kind == description.Standalone || (isOpQuery && desc.Server.Kind != description.Mongos) ||

0 commit comments

Comments
 (0)