Skip to content

Commit b50aadc

Browse files
authored
feat(NODE-5613): add awaited field to SDAM heartbeat events (#3895)
1 parent db90293 commit b50aadc

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/sdam/events.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,13 @@ export class TopologyClosedEvent {
132132
export class ServerHeartbeatStartedEvent {
133133
/** The connection id for the command */
134134
connectionId: string;
135+
/** Is true when using the streaming protocol. */
136+
awaited: boolean;
135137

136138
/** @internal */
137-
constructor(connectionId: string) {
139+
constructor(connectionId: string, awaited: boolean) {
138140
this.connectionId = connectionId;
141+
this.awaited = awaited;
139142
}
140143
}
141144

@@ -151,12 +154,15 @@ export class ServerHeartbeatSucceededEvent {
151154
duration: number;
152155
/** The command reply */
153156
reply: Document;
157+
/** Is true when using the streaming protocol. */
158+
awaited: boolean;
154159

155160
/** @internal */
156-
constructor(connectionId: string, duration: number, reply: Document | null) {
161+
constructor(connectionId: string, duration: number, reply: Document | null, awaited: boolean) {
157162
this.connectionId = connectionId;
158163
this.duration = duration;
159164
this.reply = reply ?? {};
165+
this.awaited = awaited;
160166
}
161167
}
162168

@@ -172,11 +178,14 @@ export class ServerHeartbeatFailedEvent {
172178
duration: number;
173179
/** The command failure */
174180
failure: Error;
181+
/** Is true when using the streaming protocol. */
182+
awaited: boolean;
175183

176184
/** @internal */
177-
constructor(connectionId: string, duration: number, failure: Error) {
185+
constructor(connectionId: string, duration: number, failure: Error, awaited: boolean) {
178186
this.connectionId = connectionId;
179187
this.duration = duration;
180188
this.failure = failure;
189+
this.awaited = awaited;
181190
}
182191
}

src/sdam/monitor.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,25 @@ function resetMonitorState(monitor: Monitor) {
209209

210210
function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
211211
let start = now();
212-
monitor.emit(Server.SERVER_HEARTBEAT_STARTED, new ServerHeartbeatStartedEvent(monitor.address));
212+
const topologyVersion = monitor[kServer].description.topologyVersion;
213+
const isAwaitable = topologyVersion != null;
214+
monitor.emit(
215+
Server.SERVER_HEARTBEAT_STARTED,
216+
new ServerHeartbeatStartedEvent(monitor.address, isAwaitable)
217+
);
213218

214219
function failureHandler(err: Error) {
215220
monitor[kConnection]?.destroy({ force: true });
216221
monitor[kConnection] = undefined;
217222

218223
monitor.emit(
219224
Server.SERVER_HEARTBEAT_FAILED,
220-
new ServerHeartbeatFailedEvent(monitor.address, calculateDurationInMs(start), err)
225+
new ServerHeartbeatFailedEvent(
226+
monitor.address,
227+
calculateDurationInMs(start),
228+
err,
229+
isAwaitable
230+
)
221231
);
222232

223233
const error = !(err instanceof MongoError)
@@ -237,8 +247,6 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
237247
const { serverApi, helloOk } = connection;
238248
const connectTimeoutMS = monitor.options.connectTimeoutMS;
239249
const maxAwaitTimeMS = monitor.options.heartbeatFrequencyMS;
240-
const topologyVersion = monitor[kServer].description.topologyVersion;
241-
const isAwaitable = topologyVersion != null;
242250

243251
const cmd = {
244252
[serverApi?.version || helloOk ? 'hello' : LEGACY_HELLO_COMMAND]: 1,
@@ -278,17 +286,18 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
278286
const duration =
279287
isAwaitable && rttPinger ? rttPinger.roundTripTime : calculateDurationInMs(start);
280288

289+
const awaited = isAwaitable && hello.topologyVersion != null;
281290
monitor.emit(
282291
Server.SERVER_HEARTBEAT_SUCCEEDED,
283-
new ServerHeartbeatSucceededEvent(monitor.address, duration, hello)
292+
new ServerHeartbeatSucceededEvent(monitor.address, duration, hello, awaited)
284293
);
285294

286295
// if we are using the streaming protocol then we immediately issue another `started`
287296
// event, otherwise the "check" is complete and return to the main monitor loop
288-
if (isAwaitable && hello.topologyVersion) {
297+
if (awaited) {
289298
monitor.emit(
290299
Server.SERVER_HEARTBEAT_STARTED,
291-
new ServerHeartbeatStartedEvent(monitor.address)
300+
new ServerHeartbeatStartedEvent(monitor.address, true)
292301
);
293302
start = now();
294303
} else {
@@ -324,7 +333,12 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
324333
monitor[kConnection] = conn;
325334
monitor.emit(
326335
Server.SERVER_HEARTBEAT_SUCCEEDED,
327-
new ServerHeartbeatSucceededEvent(monitor.address, calculateDurationInMs(start), conn.hello)
336+
new ServerHeartbeatSucceededEvent(
337+
monitor.address,
338+
calculateDurationInMs(start),
339+
conn.hello,
340+
false
341+
)
328342
);
329343

330344
callback(undefined, conn.hello);

0 commit comments

Comments
 (0)