@@ -27,8 +27,6 @@ const kConnection = Symbol('connection');
27
27
/** @internal */
28
28
const kCancellationToken = Symbol ( 'cancellationToken' ) ;
29
29
/** @internal */
30
- const kRTTPinger = Symbol ( 'rttPinger' ) ;
31
- /** @internal */
32
30
const kRoundTripTime = Symbol ( 'roundTripTime' ) ;
33
31
34
32
const STATE_IDLE = 'idle' ;
@@ -100,7 +98,7 @@ export class Monitor extends TypedEventEmitter<MonitorEvents> {
100
98
[ kCancellationToken ] : CancellationToken ;
101
99
/** @internal */
102
100
[ kMonitorId ] ?: MonitorInterval ;
103
- [ kRTTPinger ] ?: RTTPinger ;
101
+ rttPinger ?: RTTPinger ;
104
102
105
103
get connection ( ) : Connection | undefined {
106
104
return this [ kConnection ] ;
@@ -219,8 +217,8 @@ function resetMonitorState(monitor: Monitor) {
219
217
monitor [ kMonitorId ] ?. stop ( ) ;
220
218
monitor [ kMonitorId ] = undefined ;
221
219
222
- monitor [ kRTTPinger ] ?. close ( ) ;
223
- monitor [ kRTTPinger ] = undefined ;
220
+ monitor . rttPinger ?. close ( ) ;
221
+ monitor . rttPinger = undefined ;
224
222
225
223
monitor [ kCancellationToken ] . emit ( 'cancel' ) ;
226
224
@@ -294,8 +292,8 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
294
292
}
295
293
: { socketTimeoutMS : connectTimeoutMS } ;
296
294
297
- if ( isAwaitable && monitor [ kRTTPinger ] == null ) {
298
- monitor [ kRTTPinger ] = new RTTPinger (
295
+ if ( isAwaitable && monitor . rttPinger == null ) {
296
+ monitor . rttPinger = new RTTPinger (
299
297
monitor [ kCancellationToken ] ,
300
298
Object . assign (
301
299
{ heartbeatFrequencyMS : monitor . options . heartbeatFrequencyMS } ,
@@ -314,9 +312,10 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
314
312
hello . isWritablePrimary = hello [ LEGACY_HELLO_COMMAND ] ;
315
313
}
316
314
317
- const rttPinger = monitor [ kRTTPinger ] ;
318
315
const duration =
319
- isAwaitable && rttPinger ? rttPinger . roundTripTime : calculateDurationInMs ( start ) ;
316
+ isAwaitable && monitor . rttPinger
317
+ ? monitor . rttPinger . roundTripTime
318
+ : calculateDurationInMs ( start ) ;
320
319
321
320
monitor . emit (
322
321
Server . SERVER_HEARTBEAT_SUCCEEDED ,
@@ -332,8 +331,8 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
332
331
) ;
333
332
start = now ( ) ;
334
333
} else {
335
- monitor [ kRTTPinger ] ?. close ( ) ;
336
- monitor [ kRTTPinger ] = undefined ;
334
+ monitor . rttPinger ?. close ( ) ;
335
+ monitor . rttPinger = undefined ;
337
336
338
337
callback ( undefined , hello ) ;
339
338
}
@@ -430,8 +429,7 @@ export interface RTTPingerOptions extends ConnectionOptions {
430
429
431
430
/** @internal */
432
431
export class RTTPinger {
433
- /** @internal */
434
- [ kConnection ] ?: Connection ;
432
+ connection ?: Connection ;
435
433
/** @internal */
436
434
[ kCancellationToken ] : CancellationToken ;
437
435
/** @internal */
@@ -441,7 +439,7 @@ export class RTTPinger {
441
439
closed : boolean ;
442
440
443
441
constructor ( cancellationToken : CancellationToken , options : RTTPingerOptions ) {
444
- this [ kConnection ] = undefined ;
442
+ this . connection = undefined ;
445
443
this [ kCancellationToken ] = cancellationToken ;
446
444
this [ kRoundTripTime ] = 0 ;
447
445
this . closed = false ;
@@ -458,8 +456,8 @@ export class RTTPinger {
458
456
this . closed = true ;
459
457
clearTimeout ( this [ kMonitorId ] ) ;
460
458
461
- this [ kConnection ] ?. destroy ( { force : true } ) ;
462
- this [ kConnection ] = undefined ;
459
+ this . connection ?. destroy ( { force : true } ) ;
460
+ this . connection = undefined ;
463
461
}
464
462
}
465
463
@@ -478,8 +476,8 @@ function measureRoundTripTime(rttPinger: RTTPinger, options: RTTPingerOptions) {
478
476
return ;
479
477
}
480
478
481
- if ( rttPinger [ kConnection ] == null ) {
482
- rttPinger [ kConnection ] = conn ;
479
+ if ( rttPinger . connection == null ) {
480
+ rttPinger . connection = conn ;
483
481
}
484
482
485
483
rttPinger [ kRoundTripTime ] = calculateDurationInMs ( start ) ;
@@ -489,11 +487,11 @@ function measureRoundTripTime(rttPinger: RTTPinger, options: RTTPingerOptions) {
489
487
) ;
490
488
}
491
489
492
- const connection = rttPinger [ kConnection ] ;
490
+ const connection = rttPinger . connection ;
493
491
if ( connection == null ) {
494
492
connect ( options , ( err , conn ) => {
495
493
if ( err ) {
496
- rttPinger [ kConnection ] = undefined ;
494
+ rttPinger . connection = undefined ;
497
495
rttPinger [ kRoundTripTime ] = 0 ;
498
496
return ;
499
497
}
@@ -504,15 +502,17 @@ function measureRoundTripTime(rttPinger: RTTPinger, options: RTTPingerOptions) {
504
502
return ;
505
503
}
506
504
507
- connection . command ( ns ( 'admin.$cmd' ) , { [ LEGACY_HELLO_COMMAND ] : 1 } , undefined , err => {
508
- if ( err ) {
509
- rttPinger [ kConnection ] = undefined ;
505
+ const commandName =
506
+ connection . serverApi ?. version || connection . helloOk ? 'hello' : LEGACY_HELLO_COMMAND ;
507
+ connection . commandAsync ( ns ( 'admin.$cmd' ) , { [ commandName ] : 1 } , undefined ) . then (
508
+ ( ) => measureAndReschedule ( ) ,
509
+ ( ) => {
510
+ rttPinger . connection ?. destroy ( { force : true } ) ;
511
+ rttPinger . connection = undefined ;
510
512
rttPinger [ kRoundTripTime ] = 0 ;
511
513
return ;
512
514
}
513
-
514
- measureAndReschedule ( ) ;
515
- } ) ;
515
+ ) ;
516
516
}
517
517
518
518
/**
0 commit comments