1
1
import { clearTimeout , setTimeout } from 'timers' ;
2
2
3
+ import { type Document } from './bson' ;
3
4
import { MongoInvalidArgumentError , MongoOperationTimeoutError , MongoRuntimeError } from './error' ;
4
5
import { type ClientSession } from './sessions' ;
5
6
import { csotMin , noop } from './utils' ;
@@ -171,8 +172,6 @@ export abstract class TimeoutContext {
171
172
172
173
abstract get clearServerSelectionTimeout ( ) : boolean ;
173
174
174
- abstract get clearConnectionCheckoutTimeout ( ) : boolean ;
175
-
176
175
abstract get timeoutForSocketWrite ( ) : Timeout | null ;
177
176
178
177
abstract get timeoutForSocketRead ( ) : Timeout | null ;
@@ -185,6 +184,10 @@ export abstract class TimeoutContext {
185
184
186
185
/** Returns a new instance of the TimeoutContext, with all timeouts refreshed and restarted. */
187
186
abstract refreshed ( ) : TimeoutContext ;
187
+
188
+ abstract addMaxTimeMSToCommand ( command : Document , options : { omitMaxTimeMS ?: boolean } ) : void ;
189
+
190
+ abstract getSocketTimeoutMS ( ) : number | undefined ;
188
191
}
189
192
190
193
/** @internal */
@@ -193,7 +196,6 @@ export class CSOTTimeoutContext extends TimeoutContext {
193
196
serverSelectionTimeoutMS : number ;
194
197
socketTimeoutMS ?: number ;
195
198
196
- clearConnectionCheckoutTimeout : boolean ;
197
199
clearServerSelectionTimeout : boolean ;
198
200
199
201
private _serverSelectionTimeout ?: Timeout | null ;
@@ -212,7 +214,6 @@ export class CSOTTimeoutContext extends TimeoutContext {
212
214
this . socketTimeoutMS = options . socketTimeoutMS ;
213
215
214
216
this . clearServerSelectionTimeout = false ;
215
- this . clearConnectionCheckoutTimeout = true ;
216
217
}
217
218
218
219
get maxTimeMS ( ) : number {
@@ -325,19 +326,27 @@ export class CSOTTimeoutContext extends TimeoutContext {
325
326
override refreshed ( ) : CSOTTimeoutContext {
326
327
return new CSOTTimeoutContext ( this ) ;
327
328
}
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
+ }
328
339
}
329
340
330
341
/** @internal */
331
342
export class LegacyTimeoutContext extends TimeoutContext {
332
343
options : LegacyTimeoutContextOptions ;
333
344
clearServerSelectionTimeout : boolean ;
334
- clearConnectionCheckoutTimeout : boolean ;
335
345
336
346
constructor ( options : LegacyTimeoutContextOptions ) {
337
347
super ( ) ;
338
348
this . options = options ;
339
349
this . clearServerSelectionTimeout = true ;
340
- this . clearConnectionCheckoutTimeout = true ;
341
350
}
342
351
343
352
csotEnabled ( ) : this is CSOTTimeoutContext {
@@ -379,4 +388,12 @@ export class LegacyTimeoutContext extends TimeoutContext {
379
388
override refreshed ( ) : LegacyTimeoutContext {
380
389
return new LegacyTimeoutContext ( this . options ) ;
381
390
}
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
+ }
382
399
}
0 commit comments