11import { clearTimeout , setTimeout } from 'timers' ;
22
3+ import { type Document } from './bson' ;
34import { MongoInvalidArgumentError , MongoOperationTimeoutError , MongoRuntimeError } from './error' ;
45import { type ClientSession } from './sessions' ;
56import { csotMin , noop } from './utils' ;
@@ -171,8 +172,6 @@ export abstract class TimeoutContext {
171172
172173 abstract get clearServerSelectionTimeout ( ) : boolean ;
173174
174- abstract get clearConnectionCheckoutTimeout ( ) : boolean ;
175-
176175 abstract get timeoutForSocketWrite ( ) : Timeout | null ;
177176
178177 abstract get timeoutForSocketRead ( ) : Timeout | null ;
@@ -185,6 +184,10 @@ export abstract class TimeoutContext {
185184
186185 /** Returns a new instance of the TimeoutContext, with all timeouts refreshed and restarted. */
187186 abstract refreshed ( ) : TimeoutContext ;
187+
188+ abstract addMaxTimeMSToCommand ( command : Document , options : { omitMaxTimeMS ?: boolean } ) : void ;
189+
190+ abstract getSocketTimeoutMS ( ) : number | undefined ;
188191}
189192
190193/** @internal */
@@ -193,7 +196,6 @@ export class CSOTTimeoutContext extends TimeoutContext {
193196 serverSelectionTimeoutMS : number ;
194197 socketTimeoutMS ?: number ;
195198
196- clearConnectionCheckoutTimeout : boolean ;
197199 clearServerSelectionTimeout : boolean ;
198200
199201 private _serverSelectionTimeout ?: Timeout | null ;
@@ -212,7 +214,6 @@ export class CSOTTimeoutContext extends TimeoutContext {
212214 this . socketTimeoutMS = options . socketTimeoutMS ;
213215
214216 this . clearServerSelectionTimeout = false ;
215- this . clearConnectionCheckoutTimeout = true ;
216217 }
217218
218219 get maxTimeMS ( ) : number {
@@ -325,19 +326,27 @@ export class CSOTTimeoutContext extends TimeoutContext {
325326 override refreshed ( ) : CSOTTimeoutContext {
326327 return new CSOTTimeoutContext ( this ) ;
327328 }
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+ }
328339}
329340
330341/** @internal */
331342export class LegacyTimeoutContext extends TimeoutContext {
332343 options : LegacyTimeoutContextOptions ;
333344 clearServerSelectionTimeout : boolean ;
334- clearConnectionCheckoutTimeout : boolean ;
335345
336346 constructor ( options : LegacyTimeoutContextOptions ) {
337347 super ( ) ;
338348 this . options = options ;
339349 this . clearServerSelectionTimeout = true ;
340- this . clearConnectionCheckoutTimeout = true ;
341350 }
342351
343352 csotEnabled ( ) : this is CSOTTimeoutContext {
@@ -379,4 +388,12 @@ export class LegacyTimeoutContext extends TimeoutContext {
379388 override refreshed ( ) : LegacyTimeoutContext {
380389 return new LegacyTimeoutContext ( this . options ) ;
381390 }
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+ }
382399}
0 commit comments