Skip to content

Commit 50b8328

Browse files
committed
deprecate legacy timeout options; add logic to prefer timeoutMS
1 parent 4fd4b24 commit 50b8328

15 files changed

+73
-38
lines changed

src/connection_string.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,7 @@ export const OPTIONS = {
10921092
type: 'string'
10931093
},
10941094
socketTimeoutMS: {
1095+
deprecated: 'Please use timeoutMS instead',
10951096
default: 0,
10961097
type: 'uint'
10971098
},
@@ -1162,6 +1163,7 @@ export const OPTIONS = {
11621163
}
11631164
},
11641165
waitQueueTimeoutMS: {
1166+
deprecated: 'Please use timeoutMS instead',
11651167
default: 0,
11661168
type: 'uint'
11671169
},

src/cursor/abstract_cursor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export interface AbstractCursorOptions extends BSONSerializeOptions {
8484
/**
8585
* When applicable `maxTimeMS` controls the amount of time the initial command
8686
* that constructs a cursor should take. (ex. find, aggregate, listCollections)
87+
* @deprecated Will be removed in the next major version. Please use timeoutMS instead.
8788
*/
8889
maxTimeMS?: number;
8990
/**
@@ -721,6 +722,7 @@ export abstract class AbstractCursor<
721722
* Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher)
722723
*
723724
* @param value - Number of milliseconds to wait before aborting the query.
725+
* @deprecated Will be removed in the next major version. Please use the timeoutMS option instead.
724726
*/
725727
maxTimeMS(value: number): this {
726728
this.throwIfInitialized();

src/cursor/find_cursor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ export class FindCursor<TSchema = any> extends ExplainableCursor<TSchema> {
333333
* Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher)
334334
*
335335
* @param value - Number of milliseconds to wait before aborting the query.
336+
*@deprecated Will be removed in the next major version. Please use timeoutMS instead.
336337
*/
337338
override maxTimeMS(value: number): this {
338339
this.throwIfInitialized();

src/cursor/run_command_cursor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export class RunCommandCursor extends AbstractCursor {
4848
/**
4949
* Controls the `getMore.maxTimeMS` field. Only valid when cursor is tailable await
5050
* @param maxTimeMS - the number of milliseconds to wait for new data
51+
* @deprecated Will be removed in the next major version. Please use timeoutMS instead.
5152
*/
5253
public setMaxTimeMS(maxTimeMS: number): this {
5354
this.getMoreOptions.maxAwaitTimeMS = maxTimeMS;
@@ -56,7 +57,7 @@ export class RunCommandCursor extends AbstractCursor {
5657

5758
/**
5859
* Controls the `getMore.batchSize` field
59-
* @param maxTimeMS - the number documents to return in the `nextBatch`
60+
* @param batchSize - the number documents to return in the `nextBatch`
6061
*/
6162
public setBatchSize(batchSize: number): this {
6263
this.getMoreOptions.batchSize = batchSize;
@@ -82,7 +83,8 @@ export class RunCommandCursor extends AbstractCursor {
8283
);
8384
}
8485

85-
/** Unsupported for RunCommandCursor: maxTimeMS must be configured directly on command document */
86+
/** Unsupported for RunCommandCursor: maxTimeMS must be configured directly on command document
87+
* @deprecated Will be removed in the next major version. */
8688
public override maxTimeMS(_: number): never {
8789
throw new MongoAPIError(
8890
'maxTimeMS must be configured on the command document directly, to configure getMore.maxTimeMS use cursor.setMaxTimeMS()'

src/explain.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export type ExplainVerbosityLike = ExplainVerbosity | boolean;
2424
export interface ExplainCommandOptions {
2525
/** The explain verbosity for the command. */
2626
verbosity: ExplainVerbosity;
27-
/** The maxTimeMS setting for the command. */
27+
/** The maxTimeMS setting for the command.
28+
* @deprecated Will be removed in the next major version. Please use timeoutMS instead.*/
2829
maxTimeMS?: number;
2930
}
3031

src/mongo_client.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
152152
tlsInsecure?: boolean;
153153
/** The time in milliseconds to attempt a connection before timing out. */
154154
connectTimeoutMS?: number;
155-
/** The time in milliseconds to attempt a send or receive on a socket before the attempt times out. */
155+
/** The time in milliseconds to attempt a send or receive on a socket before the attempt times out.
156+
* @deprecated Will be removed in the next major version. Please use timeoutMS instead */
156157
socketTimeoutMS?: number;
157158
/** An array or comma-delimited string of compressors to enable network compression for communication between this client and a mongod/mongos instance. */
158159
compressors?: CompressorName[] | string;
@@ -176,7 +177,8 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
176177
maxConnecting?: number;
177178
/** The maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed. */
178179
maxIdleTimeMS?: number;
179-
/** The maximum time in milliseconds that a thread can wait for a connection to become available. */
180+
/** The maximum time in milliseconds that a thread can wait for a connection to become available.
181+
* @deprecated Will be removed in the next major version. Please use timeoutMS instead */
180182
waitQueueTimeoutMS?: number;
181183
/** Specify a read concern for the collection (only MongoDB 3.2 or higher supported) */
182184
readConcern?: ReadConcernLike;

src/operations/aggregate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export interface AggregateOptions extends Omit<CommandOperationOptions, 'explain
2626
bypassDocumentValidation?: boolean;
2727
/** Return the query as cursor, on 2.6 \> it returns as a real cursor on pre 2.6 it returns as an emulated cursor. */
2828
cursor?: Document;
29-
/** specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point. */
29+
/** specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point.
30+
* @deprecated Will be removed in the next major version. Please use timeoutMS instead.*/
3031
maxTimeMS?: number;
3132
/** The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query. */
3233
maxAwaitTimeMS?: number;

src/operations/command.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface CommandOperationOptions
4040
readConcern?: ReadConcernLike;
4141
/** Collation */
4242
collation?: CollationOptions;
43+
/**
44+
* @deprecated Will be removed in the next major version. Please use timeoutMS instead. */
4345
maxTimeMS?: number;
4446
/**
4547
* Comment to apply to the operation.

src/operations/count.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export interface CountOptions extends CommandOperationOptions {
1313
skip?: number;
1414
/** The maximum amounts to count before aborting. */
1515
limit?: number;
16-
/** Number of milliseconds to wait before aborting the query. */
16+
/** Number of milliseconds to wait before aborting the query.
17+
* @deprecated Will be removed in the next major version. Please use timeoutMS instead. */
1718
maxTimeMS?: number;
1819
/** An index name hint for the query. */
1920
hint?: string | Document;

src/operations/estimated_document_count.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
1212
* The maximum amount of time to allow the operation to run.
1313
*
1414
* This option is sent only if the caller explicitly provides a value. The default is to not send a value.
15+
*@deprecated Will be removed in the next major version. Please use timeoutMS instead.
1516
*/
1617
maxTimeMS?: number;
1718
}

src/operations/find.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export interface FindOptions<TSchema extends Document = Document>
4848
min?: Document;
4949
/** The exclusive upper bound for a specific index */
5050
max?: Document;
51-
/** Number of milliseconds to wait before aborting the query. */
51+
/** Number of milliseconds to wait before aborting the query.
52+
* @deprecated Will be removed in the next major version. Please use timeoutMS instead. */
5253
maxTimeMS?: number;
5354
/** The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query. Requires `tailable` and `awaitData` to be true */
5455
maxAwaitTimeMS?: number;
@@ -77,7 +78,6 @@ export interface FindOptions<TSchema extends Document = Document>
7778
* @deprecated This API is deprecated in favor of `collection.find().explain()`.
7879
*/
7980
explain?: ExplainOptions['explain'];
80-
/** @internal*/
8181
timeoutMode?: CursorTimeoutMode;
8282
}
8383

src/operations/get_more.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface GetMoreOptions extends OperationOptions {
1717
* getMore only supports 'comment' in server versions 4.4 and above.
1818
*/
1919
comment?: unknown;
20-
/** Number of milliseconds to wait before aborting the query. */
20+
/** Number of milliseconds to wait before aborting the query.
2121
maxTimeMS?: number;
2222
/** TODO(NODE-4413): Address bug with maxAwaitTimeMS not being passed in from the cursor correctly */
2323
maxAwaitTimeMS?: number;

src/sessions.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,29 @@ export class ClientSession
487487
maxTimeMS?: number;
488488
} = { commitTransaction: 1 };
489489

490+
const timeoutMS =
491+
typeof options?.timeoutMS === 'number'
492+
? options.timeoutMS
493+
: typeof this.timeoutMS === 'number'
494+
? this.timeoutMS
495+
: null;
496+
490497
const wc = this.transaction.options.writeConcern ?? this.clientOptions?.writeConcern;
491498
if (wc != null) {
492-
WriteConcern.apply(command, { wtimeoutMS: 10000, w: 'majority', ...wc });
499+
if (timeoutMS == null && this.timeoutContext == null) {
500+
WriteConcern.apply(command, { wtimeoutMS: 10000, w: 'majority', ...wc });
501+
} else {
502+
WriteConcern.apply(command, { w: 'majority', ...wc, wtimeoutMS: undefined });
503+
}
493504
}
494505

495506
if (this.transaction.state === TxnState.TRANSACTION_COMMITTED || this.commitAttempted) {
496507
WriteConcern.apply(command, { wtimeoutMS: 10000, ...wc, w: 'majority' });
508+
if (timeoutMS == null && this.timeoutContext == null) {
509+
WriteConcern.apply(command, { wtimeoutMS: 10000, ...wc, w: 'majority' });
510+
} else {
511+
WriteConcern.apply(command, { w: 'majority', ...wc, wtimeoutMS: undefined });
512+
}
497513
}
498514

499515
if (typeof this.transaction.options.maxTimeMS === 'number') {
@@ -510,13 +526,6 @@ export class ClientSession
510526
bypassPinningCheck: true
511527
});
512528

513-
const timeoutMS =
514-
typeof options?.timeoutMS === 'number'
515-
? options.timeoutMS
516-
: typeof this.timeoutMS === 'number'
517-
? this.timeoutMS
518-
: null;
519-
520529
const timeoutContext =
521530
this.timeoutContext ??
522531
(typeof timeoutMS === 'number'
@@ -616,21 +625,6 @@ export class ClientSession
616625
recoveryToken?: Document;
617626
} = { abortTransaction: 1 };
618627

619-
const wc = this.transaction.options.writeConcern ?? this.clientOptions?.writeConcern;
620-
if (wc != null) {
621-
WriteConcern.apply(command, { wtimeoutMS: 10000, w: 'majority', ...wc });
622-
}
623-
624-
if (this.transaction.recoveryToken) {
625-
command.recoveryToken = this.transaction.recoveryToken;
626-
}
627-
628-
const operation = new RunAdminCommandOperation(command, {
629-
session: this,
630-
readPreference: ReadPreference.primary,
631-
bypassPinningCheck: true
632-
});
633-
634628
const timeoutMS =
635629
typeof options?.timeoutMS === 'number'
636630
? options.timeoutMS
@@ -649,6 +643,21 @@ export class ClientSession
649643
})
650644
: null;
651645

646+
const wc = this.transaction.options.writeConcern ?? this.clientOptions?.writeConcern;
647+
if (wc != null && timeoutMS == null) {
648+
WriteConcern.apply(command, { wtimeoutMS: 10000, w: 'majority', ...wc });
649+
}
650+
651+
if (this.transaction.recoveryToken) {
652+
command.recoveryToken = this.transaction.recoveryToken;
653+
}
654+
655+
const operation = new RunAdminCommandOperation(command, {
656+
session: this,
657+
readPreference: ReadPreference.primary,
658+
bypassPinningCheck: true
659+
});
660+
652661
try {
653662
await executeOperation(this.client, operation, timeoutContext);
654663
this.unpin();

src/utils.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,20 +516,31 @@ export function resolveOptions<T extends CommandOperationOptions>(
516516
): T {
517517
const result: T = Object.assign({}, options, resolveBSONOptions(options, parent));
518518

519+
const timeoutMS = options?.timeoutMS ?? parent?.timeoutMS;
519520
// Users cannot pass a readConcern/writeConcern to operations in a transaction
520521
const session = options?.session;
522+
521523
if (!session?.inTransaction()) {
522524
const readConcern = ReadConcern.fromOptions(options) ?? parent?.readConcern;
523525
if (readConcern) {
524526
result.readConcern = readConcern;
525527
}
526528

527-
const writeConcern = WriteConcern.fromOptions(options) ?? parent?.writeConcern;
529+
let writeConcern = WriteConcern.fromOptions(options) ?? parent?.writeConcern;
528530
if (writeConcern) {
531+
if (timeoutMS != null) {
532+
writeConcern = WriteConcern.fromOptions({
533+
...writeConcern,
534+
wtimeout: undefined,
535+
wtimeoutMS: undefined
536+
});
537+
}
529538
result.writeConcern = writeConcern;
530539
}
531540
}
532541

542+
result.timeoutMS = timeoutMS;
543+
533544
const readPreference = ReadPreference.fromOptions(options) ?? parent?.readPreference;
534545
if (readPreference) {
535546
result.readPreference = readPreference;
@@ -542,8 +553,6 @@ export function resolveOptions<T extends CommandOperationOptions>(
542553
);
543554
}
544555

545-
result.timeoutMS = options?.timeoutMS ?? parent?.timeoutMS;
546-
547556
return result;
548557
}
549558

src/write_concern.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export interface WriteConcernOptions {
1515
export interface WriteConcernSettings {
1616
/** The write concern */
1717
w?: W;
18-
/** The write concern timeout */
18+
/** The write concern timeout
19+
* @deprecated Will be removed in the next major version. Please use timeoutMS */
1920
wtimeoutMS?: number;
2021
/** The journal write concern */
2122
journal?: boolean;
@@ -65,7 +66,8 @@ export class WriteConcern {
6566
readonly w?: W;
6667
/** Request acknowledgment that the write operation has been written to the on-disk journal */
6768
readonly journal?: boolean;
68-
/** Specify a time limit to prevent write operations from blocking indefinitely */
69+
/** Specify a time limit to prevent write operations from blocking indefinitely
70+
* @deprecated Will be removed in the next major version. Please use timeoutMS */
6971
readonly wtimeoutMS?: number;
7072
/**
7173
* Specify a time limit to prevent write operations from blocking indefinitely.

0 commit comments

Comments
 (0)