Skip to content

Commit 3ee3eaf

Browse files
Consolidate logic
1 parent fd7870e commit 3ee3eaf

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

src/cmap/connection.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
427427
...options
428428
};
429429

430-
if (!options.omitMaxTimeMS) {
431-
const maxTimeMS = options.timeoutContext?.maxTimeMS;
432-
if (maxTimeMS && maxTimeMS > 0 && Number.isFinite(maxTimeMS)) cmd.maxTimeMS = maxTimeMS;
433-
}
430+
options.timeoutContext?.addMaxTimeMSToCommand(cmd, options);
434431

435432
const message = this.supportsOpMsg
436433
? new OpMsgRequest(db, cmd, commandOptions)
@@ -446,13 +443,11 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
446443
): AsyncGenerator<MongoDBResponse> {
447444
this.throwIfAborted();
448445

449-
if (options.timeoutContext?.csotEnabled()) {
450-
this.socket.setTimeout(0);
451-
} else if (typeof options.socketTimeoutMS === 'number') {
452-
this.socket.setTimeout(options.socketTimeoutMS);
453-
} else if (this.socketTimeoutMS !== 0) {
454-
this.socket.setTimeout(this.socketTimeoutMS);
455-
}
446+
const timeout =
447+
options.socketTimeoutMS ??
448+
options?.timeoutContext?.getSocketTimeoutMS() ??
449+
this.socketTimeoutMS;
450+
this.socket.setTimeout(timeout);
456451

457452
try {
458453
await this.writeCommand(message, {
@@ -487,11 +482,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
487482
yield document;
488483
this.throwIfAborted();
489484

490-
if (typeof options.socketTimeoutMS === 'number') {
491-
this.socket.setTimeout(options.socketTimeoutMS);
492-
} else if (this.socketTimeoutMS !== 0) {
493-
this.socket.setTimeout(this.socketTimeoutMS);
494-
}
485+
this.socket.setTimeout(timeout);
495486
}
496487
} finally {
497488
this.socket.setTimeout(0);

src/cursor/abstract_cursor.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,12 +1187,16 @@ export class CursorTimeoutContext extends TimeoutContext {
11871187
override get maxTimeMS(): number | null {
11881188
return this.timeoutContext.maxTimeMS;
11891189
}
1190-
11911190
get timeoutMS(): number | null {
11921191
return this.timeoutContext.csotEnabled() ? this.timeoutContext.timeoutMS : null;
11931192
}
1194-
11951193
override refreshed(): CursorTimeoutContext {
11961194
return new CursorTimeoutContext(this.timeoutContext.refreshed(), this.owner);
11971195
}
1196+
override addMaxTimeMSToCommand(command: Document, options: { omitMaxTimeMS?: boolean }): void {
1197+
this.timeoutContext.addMaxTimeMSToCommand(command, options);
1198+
}
1199+
override getSocketTimeoutMS(): number | undefined {
1200+
return this.timeoutContext.getSocketTimeoutMS();
1201+
}
11981202
}

src/timeout.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { type Document } from 'bson';
12
import { clearTimeout, setTimeout } from 'timers';
23

34
import { MongoInvalidArgumentError, MongoOperationTimeoutError, MongoRuntimeError } from './error';
@@ -183,6 +184,10 @@ export abstract class TimeoutContext {
183184

184185
/** Returns a new instance of the TimeoutContext, with all timeouts refreshed and restarted. */
185186
abstract refreshed(): TimeoutContext;
187+
188+
abstract addMaxTimeMSToCommand(command: Document, options: { omitMaxTimeMS?: boolean }): void;
189+
190+
abstract getSocketTimeoutMS(): number | undefined;
186191
}
187192

188193
/** @internal */
@@ -321,6 +326,16 @@ export class CSOTTimeoutContext extends TimeoutContext {
321326
override refreshed(): CSOTTimeoutContext {
322327
return new CSOTTimeoutContext(this);
323328
}
329+
330+
override addMaxTimeMSToCommand(command: Document, options: { omitMaxTimeMS?: boolean }): void {
331+
if (options.omitMaxTimeMS) return;
332+
const maxTimeMS = this.remainingTimeMS - this.minRoundTripTime;
333+
if (maxTimeMS > 0 && Number.isFinite(maxTimeMS)) command.maxTimeMS = maxTimeMS;
334+
}
335+
336+
override getSocketTimeoutMS(): number | undefined {
337+
return 0;
338+
}
324339
}
325340

326341
/** @internal */
@@ -373,4 +388,12 @@ export class LegacyTimeoutContext extends TimeoutContext {
373388
override refreshed(): LegacyTimeoutContext {
374389
return new LegacyTimeoutContext(this.options);
375390
}
391+
392+
override addMaxTimeMSToCommand(_command: Document, _options: { omitMaxTimeMS?: boolean }): void {
393+
// No max timeMS is added to commands in legacy timeout mode.
394+
}
395+
396+
override getSocketTimeoutMS(): number | undefined {
397+
return this.options.socketTimeoutMS;
398+
}
376399
}

0 commit comments

Comments
 (0)