MCPcopy
hub / github.com/mongodb/node-mongodb-native / onHeartbeatSucceeded

Function onHeartbeatSucceeded

src/sdam/monitor.ts:295–336  ·  view source on GitHub ↗
(hello: Document)

Source from the content-addressed store, hash-verified

293 }
294
295 function onHeartbeatSucceeded(hello: Document) {
296 if (!('isWritablePrimary' in hello)) {
297 // Provide hello-style response document.
298 hello.isWritablePrimary = hello[LEGACY_HELLO_COMMAND];
299 }
300
301 // NOTE: here we use the latestRtt as this measurement corresponds with the value
302 // obtained for this successful heartbeat, if there is no latestRtt, then we calculate the
303 // duration
304 const duration =
305 isAwaitable && monitor.rttPinger
306 ? (monitor.rttPinger.latestRtt ?? calculateDurationInMs(start))
307 : calculateDurationInMs(start);
308
309 monitor.addRttSample(duration);
310
311 monitor.emitAndLogHeartbeat(
312 Server.SERVER_HEARTBEAT_SUCCEEDED,
313 monitor.server.topology.s.id,
314 hello.connectionId,
315 new ServerHeartbeatSucceededEvent(monitor.address, duration, hello, isAwaitable)
316 );
317
318 if (isAwaitable) {
319 // If we are using the streaming protocol then we immediately issue another 'started'
320 // event, otherwise the "check" is complete and return to the main monitor loop
321 monitor.emitAndLogHeartbeat(
322 Server.SERVER_HEARTBEAT_STARTED,
323 monitor.server.topology.s.id,
324 undefined,
325 new ServerHeartbeatStartedEvent(monitor.address, true)
326 );
327 // We have not actually sent an outgoing handshake, but when we get the next response we
328 // want the duration to reflect the time since we last heard from the server
329 start = processTimeMS();
330 } else {
331 monitor.rttPinger?.close();
332 monitor.rttPinger = undefined;
333
334 callback(undefined, hello);
335 }
336 }
337
338 const { connection } = monitor;
339 if (connection && !connection.closed) {

Callers 1

checkServerFunction · 0.85

Calls 6

calculateDurationInMsFunction · 0.90
processTimeMSFunction · 0.90
addRttSampleMethod · 0.80
emitAndLogHeartbeatMethod · 0.80
callbackFunction · 0.50
closeMethod · 0.45

Tested by

no test coverage detected