Skip to content

Commit 6edcc1b

Browse files
committed
add logic to prefer timeoutMS over wtimeoutMS
1 parent 54c72aa commit 6edcc1b

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed

src/sessions.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,28 @@ export class ClientSession
482482
maxTimeMS?: number;
483483
} = { commitTransaction: 1 };
484484

485+
const timeoutMS =
486+
typeof options?.timeoutMS === 'number'
487+
? options.timeoutMS
488+
: typeof this.timeoutMS === 'number'
489+
? this.timeoutMS
490+
: null;
491+
485492
const wc = this.transaction.options.writeConcern ?? this.clientOptions?.writeConcern;
486493
if (wc != null) {
487-
WriteConcern.apply(command, { wtimeoutMS: 10000, w: 'majority', ...wc });
494+
if (timeoutMS == null && this.timeoutContext == null) {
495+
WriteConcern.apply(command, { wtimeoutMS: 10000, w: 'majority', ...wc });
496+
} else {
497+
WriteConcern.apply(command, { w: 'majority', ...wc, wtimeoutMS: undefined });
498+
}
488499
}
489500

490501
if (this.transaction.state === TxnState.TRANSACTION_COMMITTED) {
491-
WriteConcern.apply(command, { wtimeoutMS: 10000, ...wc, w: 'majority' });
502+
if (timeoutMS == null && this.timeoutContext == null) {
503+
WriteConcern.apply(command, { wtimeoutMS: 10000, ...wc, w: 'majority' });
504+
} else {
505+
WriteConcern.apply(command, { w: 'majority', ...wc, wtimeoutMS: undefined });
506+
}
492507
}
493508

494509
if (typeof this.transaction.options.maxTimeMS === 'number') {
@@ -505,13 +520,6 @@ export class ClientSession
505520
bypassPinningCheck: true
506521
});
507522

508-
const timeoutMS =
509-
typeof options?.timeoutMS === 'number'
510-
? options.timeoutMS
511-
: typeof this.timeoutMS === 'number'
512-
? this.timeoutMS
513-
: null;
514-
515523
const timeoutContext =
516524
this.timeoutContext ??
517525
(typeof timeoutMS === 'number'
@@ -601,21 +609,6 @@ export class ClientSession
601609
recoveryToken?: Document;
602610
} = { abortTransaction: 1 };
603611

604-
const wc = this.transaction.options.writeConcern ?? this.clientOptions?.writeConcern;
605-
if (wc != null) {
606-
WriteConcern.apply(command, { wtimeoutMS: 10000, w: 'majority', ...wc });
607-
}
608-
609-
if (this.transaction.recoveryToken) {
610-
command.recoveryToken = this.transaction.recoveryToken;
611-
}
612-
613-
const operation = new RunAdminCommandOperation(command, {
614-
session: this,
615-
readPreference: ReadPreference.primary,
616-
bypassPinningCheck: true
617-
});
618-
619612
const timeoutMS =
620613
typeof options?.timeoutMS === 'number'
621614
? options.timeoutMS
@@ -634,6 +627,21 @@ export class ClientSession
634627
})
635628
: null;
636629

630+
const wc = this.transaction.options.writeConcern ?? this.clientOptions?.writeConcern;
631+
if (wc != null && timeoutMS == null) {
632+
WriteConcern.apply(command, { wtimeoutMS: 10000, w: 'majority', ...wc });
633+
}
634+
635+
if (this.transaction.recoveryToken) {
636+
command.recoveryToken = this.transaction.recoveryToken;
637+
}
638+
639+
const operation = new RunAdminCommandOperation(command, {
640+
session: this,
641+
readPreference: ReadPreference.primary,
642+
bypassPinningCheck: true
643+
});
644+
637645
try {
638646
await executeOperation(this.client, operation, timeoutContext);
639647
this.unpin();

src/utils.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -542,20 +542,36 @@ export function resolveOptions<T extends CommandOperationOptions>(
542542
): T {
543543
const result: T = Object.assign({}, options, resolveBSONOptions(options, parent));
544544

545+
const timeoutMS = options?.timeoutMS ?? parent?.timeoutMS;
545546
// Users cannot pass a readConcern/writeConcern to operations in a transaction
546547
const session = options?.session;
548+
547549
if (!session?.inTransaction()) {
548550
const readConcern = ReadConcern.fromOptions(options) ?? parent?.readConcern;
549551
if (readConcern) {
550552
result.readConcern = readConcern;
551553
}
552554

553-
const writeConcern = WriteConcern.fromOptions(options) ?? parent?.writeConcern;
555+
let writeConcern = WriteConcern.fromOptions(options) ?? parent?.writeConcern;
554556
if (writeConcern) {
555-
result.writeConcern = writeConcern;
557+
if (timeoutMS != null) {
558+
result.writeConcern = writeConcern;
559+
} else {
560+
const matchOptions = new Set(['wtimeout', 'wtimeoutMS']);
561+
const writeConcernKeys = Object.keys(writeConcern);
562+
if (writeConcernKeys.length > 2 && writeConcernKeys.some(k => !matchOptions.has(k))) {
563+
writeConcern = WriteConcern.fromOptions({
564+
...writeConcern,
565+
wtimeout: undefined,
566+
wtimeoutMS: undefined
567+
});
568+
}
569+
}
556570
}
557571
}
558572

573+
result.timeoutMS = timeoutMS;
574+
559575
const readPreference = ReadPreference.fromOptions(options) ?? parent?.readPreference;
560576
if (readPreference) {
561577
result.readPreference = readPreference;
@@ -568,15 +584,6 @@ export function resolveOptions<T extends CommandOperationOptions>(
568584
);
569585
}
570586

571-
result.timeoutMS = options?.timeoutMS ?? parent?.timeoutMS;
572-
if (result.timeoutMS != null && result.writeConcern) {
573-
const matchOptions = new Set(['wtimeout', 'wtimeoutMS']);
574-
const writeConcernKeys = Object.keys(result.writeConcern);
575-
if (writeConcernKeys.length <= 2 && writeConcernKeys.every(k => matchOptions.has(k))) {
576-
delete result.writeConcern;
577-
}
578-
}
579-
580587
return result;
581588
}
582589

0 commit comments

Comments
 (0)