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

Function checkServer

src/sdam/monitor.ts:261–424  ·  view source on GitHub ↗
(monitor: Monitor, callback: Callback<Document | null>)

Source from the content-addressed store, hash-verified

259}
260
261function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
262 let start: number;
263 let awaited: boolean;
264 const topologyVersion = monitor.server.description.topologyVersion;
265 const isAwaitable = useStreamingProtocol(monitor, topologyVersion);
266 monitor.emitAndLogHeartbeat(
267 Server.SERVER_HEARTBEAT_STARTED,
268 monitor.server.topology.s.id,
269 undefined,
270 new ServerHeartbeatStartedEvent(monitor.address, isAwaitable)
271 );
272
273 function onHeartbeatFailed(err: Error) {
274 monitor.connection?.destroy();
275 monitor.connection = null;
276 monitor.emitAndLogHeartbeat(
277 Server.SERVER_HEARTBEAT_FAILED,
278 monitor.server.topology.s.id,
279 undefined,
280 new ServerHeartbeatFailedEvent(monitor.address, calculateDurationInMs(start), err, awaited)
281 );
282
283 const error = !(err instanceof MongoError)
284 ? new MongoError(MongoError.buildErrorMessage(err), { cause: err })
285 : err;
286 error.addErrorLabel(MongoErrorLabel.ResetPool);
287 if (error instanceof MongoNetworkTimeoutError) {
288 error.addErrorLabel(MongoErrorLabel.InterruptInUseConnections);
289 }
290
291 monitor.emit('resetServer', error);
292 callback(err);
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) {

Callers 1

monitorServerFunction · 0.85

Calls 15

processTimeMSFunction · 0.90
nsFunction · 0.90
makeSocketFunction · 0.90
makeConnectionFunction · 0.90
performInitialHandshakeFunction · 0.90
calculateDurationInMsFunction · 0.90
useStreamingProtocolFunction · 0.85
makeTopologyVersionFunction · 0.85
onHeartbeatFailedFunction · 0.85
onHeartbeatSucceededFunction · 0.85
isInCloseStateFunction · 0.85
emitAndLogHeartbeatMethod · 0.80

Tested by

no test coverage detected