@@ -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
@@ -262,8 +260,8 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
262
260
}
263
261
: { socketTimeoutMS : connectTimeoutMS } ;
264
262
265
- if ( isAwaitable && monitor [ kRTTPinger ] == null ) {
266
- monitor [ kRTTPinger ] = new RTTPinger (
263
+ if ( isAwaitable && monitor . rttPinger == null ) {
264
+ monitor . rttPinger = new RTTPinger (
267
265
monitor [ kCancellationToken ] ,
268
266
Object . assign (
269
267
{ heartbeatFrequencyMS : monitor . options . heartbeatFrequencyMS } ,
@@ -282,9 +280,10 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
282
280
hello . isWritablePrimary = hello [ LEGACY_HELLO_COMMAND ] ;
283
281
}
284
282
285
- const rttPinger = monitor [ kRTTPinger ] ;
286
283
const duration =
287
- isAwaitable && rttPinger ? rttPinger . roundTripTime : calculateDurationInMs ( start ) ;
284
+ isAwaitable && monitor . rttPinger
285
+ ? monitor . rttPinger . roundTripTime
286
+ : calculateDurationInMs ( start ) ;
288
287
289
288
const awaited = isAwaitable && hello . topologyVersion != null ;
290
289
monitor . emit (
@@ -301,8 +300,8 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
301
300
) ;
302
301
start = now ( ) ;
303
302
} else {
304
- monitor [ kRTTPinger ] ?. close ( ) ;
305
- monitor [ kRTTPinger ] = undefined ;
303
+ monitor . rttPinger ?. close ( ) ;
304
+ monitor . rttPinger = undefined ;
306
305
307
306
callback ( undefined , hello ) ;
308
307
}
@@ -399,8 +398,7 @@ export interface RTTPingerOptions extends ConnectionOptions {
399
398
400
399
/** @internal */
401
400
export class RTTPinger {
402
- /** @internal */
403
- [ kConnection ] ?: Connection ;
401
+ connection ?: Connection ;
404
402
/** @internal */
405
403
[ kCancellationToken ] : CancellationToken ;
406
404
/** @internal */
@@ -410,7 +408,7 @@ export class RTTPinger {
410
408
closed : boolean ;
411
409
412
410
constructor ( cancellationToken : CancellationToken , options : RTTPingerOptions ) {
413
- this [ kConnection ] = undefined ;
411
+ this . connection = undefined ;
414
412
this [ kCancellationToken ] = cancellationToken ;
415
413
this [ kRoundTripTime ] = 0 ;
416
414
this . closed = false ;
@@ -427,8 +425,8 @@ export class RTTPinger {
427
425
this . closed = true ;
428
426
clearTimeout ( this [ kMonitorId ] ) ;
429
427
430
- this [ kConnection ] ?. destroy ( { force : true } ) ;
431
- this [ kConnection ] = undefined ;
428
+ this . connection ?. destroy ( { force : true } ) ;
429
+ this . connection = undefined ;
432
430
}
433
431
}
434
432
@@ -447,8 +445,8 @@ function measureRoundTripTime(rttPinger: RTTPinger, options: RTTPingerOptions) {
447
445
return ;
448
446
}
449
447
450
- if ( rttPinger [ kConnection ] == null ) {
451
- rttPinger [ kConnection ] = conn ;
448
+ if ( rttPinger . connection == null ) {
449
+ rttPinger . connection = conn ;
452
450
}
453
451
454
452
rttPinger [ kRoundTripTime ] = calculateDurationInMs ( start ) ;
@@ -458,11 +456,11 @@ function measureRoundTripTime(rttPinger: RTTPinger, options: RTTPingerOptions) {
458
456
) ;
459
457
}
460
458
461
- const connection = rttPinger [ kConnection ] ;
459
+ const connection = rttPinger . connection ;
462
460
if ( connection == null ) {
463
461
connect ( options , ( err , conn ) => {
464
462
if ( err ) {
465
- rttPinger [ kConnection ] = undefined ;
463
+ rttPinger . connection = undefined ;
466
464
rttPinger [ kRoundTripTime ] = 0 ;
467
465
return ;
468
466
}
@@ -473,15 +471,17 @@ function measureRoundTripTime(rttPinger: RTTPinger, options: RTTPingerOptions) {
473
471
return ;
474
472
}
475
473
476
- connection . command ( ns ( 'admin.$cmd' ) , { [ LEGACY_HELLO_COMMAND ] : 1 } , undefined , err => {
477
- if ( err ) {
478
- rttPinger [ kConnection ] = undefined ;
474
+ const commandName =
475
+ connection . serverApi ?. version || connection . helloOk ? 'hello' : LEGACY_HELLO_COMMAND ;
476
+ connection . commandAsync ( ns ( 'admin.$cmd' ) , { [ commandName ] : 1 } , undefined ) . then (
477
+ ( ) => measureAndReschedule ( ) ,
478
+ ( ) => {
479
+ rttPinger . connection ?. destroy ( { force : true } ) ;
480
+ rttPinger . connection = undefined ;
479
481
rttPinger [ kRoundTripTime ] = 0 ;
480
482
return ;
481
483
}
482
-
483
- measureAndReschedule ( ) ;
484
- } ) ;
484
+ ) ;
485
485
}
486
486
487
487
/**
0 commit comments