@@ -26,8 +26,6 @@ const kConnection = Symbol('connection');
26
26
/** @internal */
27
27
const kCancellationToken = Symbol ( 'cancellationToken' ) ;
28
28
/** @internal */
29
- const kRTTPinger = Symbol ( 'rttPinger' ) ;
30
- /** @internal */
31
29
const kRoundTripTime = Symbol ( 'roundTripTime' ) ;
32
30
33
31
const STATE_IDLE = 'idle' ;
@@ -81,7 +79,7 @@ export class Monitor extends TypedEventEmitter<MonitorEvents> {
81
79
[ kCancellationToken ] : CancellationToken ;
82
80
/** @internal */
83
81
[ kMonitorId ] ?: MonitorInterval ;
84
- [ kRTTPinger ] ?: RTTPinger ;
82
+ rttPinger ?: RTTPinger ;
85
83
86
84
get connection ( ) : Connection | undefined {
87
85
return this [ kConnection ] ;
@@ -198,8 +196,8 @@ function resetMonitorState(monitor: Monitor) {
198
196
monitor [ kMonitorId ] ?. stop ( ) ;
199
197
monitor [ kMonitorId ] = undefined ;
200
198
201
- monitor [ kRTTPinger ] ?. close ( ) ;
202
- monitor [ kRTTPinger ] = undefined ;
199
+ monitor . rttPinger ?. close ( ) ;
200
+ monitor . rttPinger = undefined ;
203
201
204
202
monitor [ kCancellationToken ] . emit ( 'cancel' ) ;
205
203
@@ -252,8 +250,8 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
252
250
}
253
251
: { socketTimeoutMS : connectTimeoutMS } ;
254
252
255
- if ( isAwaitable && monitor [ kRTTPinger ] == null ) {
256
- monitor [ kRTTPinger ] = new RTTPinger (
253
+ if ( isAwaitable && monitor . rttPinger == null ) {
254
+ monitor . rttPinger = new RTTPinger (
257
255
monitor [ kCancellationToken ] ,
258
256
Object . assign (
259
257
{ heartbeatFrequencyMS : monitor . options . heartbeatFrequencyMS } ,
@@ -272,9 +270,10 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
272
270
hello . isWritablePrimary = hello [ LEGACY_HELLO_COMMAND ] ;
273
271
}
274
272
275
- const rttPinger = monitor [ kRTTPinger ] ;
276
273
const duration =
277
- isAwaitable && rttPinger ? rttPinger . roundTripTime : calculateDurationInMs ( start ) ;
274
+ isAwaitable && monitor . rttPinger
275
+ ? monitor . rttPinger . roundTripTime
276
+ : calculateDurationInMs ( start ) ;
278
277
279
278
monitor . emit (
280
279
Server . SERVER_HEARTBEAT_SUCCEEDED ,
@@ -290,8 +289,8 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
290
289
) ;
291
290
start = now ( ) ;
292
291
} else {
293
- monitor [ kRTTPinger ] ?. close ( ) ;
294
- monitor [ kRTTPinger ] = undefined ;
292
+ monitor . rttPinger ?. close ( ) ;
293
+ monitor . rttPinger = undefined ;
295
294
296
295
callback ( undefined , hello ) ;
297
296
}
@@ -384,7 +383,7 @@ export interface RTTPingerOptions extends ConnectionOptions {
384
383
/** @internal */
385
384
export class RTTPinger {
386
385
/** @internal */
387
- [ kConnection ] ?: Connection ;
386
+ connection ?: Connection ;
388
387
/** @internal */
389
388
[ kCancellationToken ] : CancellationToken ;
390
389
/** @internal */
@@ -394,7 +393,7 @@ export class RTTPinger {
394
393
closed : boolean ;
395
394
396
395
constructor ( cancellationToken : CancellationToken , options : RTTPingerOptions ) {
397
- this [ kConnection ] = undefined ;
396
+ this . connection = undefined ;
398
397
this [ kCancellationToken ] = cancellationToken ;
399
398
this [ kRoundTripTime ] = 0 ;
400
399
this . closed = false ;
@@ -411,8 +410,8 @@ export class RTTPinger {
411
410
this . closed = true ;
412
411
clearTimeout ( this [ kMonitorId ] ) ;
413
412
414
- this [ kConnection ] ?. destroy ( { force : true } ) ;
415
- this [ kConnection ] = undefined ;
413
+ this . connection ?. destroy ( { force : true } ) ;
414
+ this . connection = undefined ;
416
415
}
417
416
}
418
417
@@ -431,8 +430,8 @@ function measureRoundTripTime(rttPinger: RTTPinger, options: RTTPingerOptions) {
431
430
return ;
432
431
}
433
432
434
- if ( rttPinger [ kConnection ] == null ) {
435
- rttPinger [ kConnection ] = conn ;
433
+ if ( rttPinger . connection == null ) {
434
+ rttPinger . connection = conn ;
436
435
}
437
436
438
437
rttPinger [ kRoundTripTime ] = calculateDurationInMs ( start ) ;
@@ -442,11 +441,11 @@ function measureRoundTripTime(rttPinger: RTTPinger, options: RTTPingerOptions) {
442
441
) ;
443
442
}
444
443
445
- const connection = rttPinger [ kConnection ] ;
444
+ const connection = rttPinger . connection ;
446
445
if ( connection == null ) {
447
446
connect ( options , ( err , conn ) => {
448
447
if ( err ) {
449
- rttPinger [ kConnection ] = undefined ;
448
+ rttPinger . connection = undefined ;
450
449
rttPinger [ kRoundTripTime ] = 0 ;
451
450
return ;
452
451
}
@@ -457,15 +456,16 @@ function measureRoundTripTime(rttPinger: RTTPinger, options: RTTPingerOptions) {
457
456
return ;
458
457
}
459
458
460
- connection . command ( ns ( 'admin.$cmd' ) , { [ LEGACY_HELLO_COMMAND ] : 1 } , undefined , err => {
461
- if ( err ) {
462
- rttPinger [ kConnection ] = undefined ;
459
+ const commandName =
460
+ connection . serverApi ?. version || connection . helloOk ? 'hello' : LEGACY_HELLO_COMMAND ;
461
+ connection . commandAsync ( ns ( 'admin.$cmd' ) , { [ commandName ] : 1 } , undefined ) . then (
462
+ ( ) => measureAndReschedule ( ) ,
463
+ ( ) => {
464
+ rttPinger . connection ?. destroy ( { force : true } ) ;
465
+ rttPinger . connection = undefined ;
463
466
rttPinger [ kRoundTripTime ] = 0 ;
464
- return ;
465
467
}
466
-
467
- measureAndReschedule ( ) ;
468
- } ) ;
468
+ ) ;
469
469
}
470
470
471
471
/**
0 commit comments