Skip to content

Commit 95c1938

Browse files
steipeteampagent
andauthored
perf(mattermost): decode inbound websocket frames once (#127870)
Co-authored-by: Amp <[email protected]>
1 parent c7f5151 commit 95c1938

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

‎extensions/mattermost/src/mattermost/monitor-websocket.test.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ describe("mattermost websocket monitor", () => {
377377
});
378378
});
379379

380-
it("hands posted envelopes to ingress raw and keeps post_edited out", async () => {
380+
it("hands posted envelopes to ingress raw without duplicate decoding", async () => {
381381
const socket = new FakeWebSocket();
382382
const onPosted = vi.fn(async () => {});
383383
const connectOnce = createMattermostConnectOnce({
@@ -398,6 +398,9 @@ describe("mattermost websocket monitor", () => {
398398
}),
399399
},
400400
};
401+
const raw = JSON.stringify(posted);
402+
const frame = Buffer.from(raw);
403+
const decode = vi.spyOn(frame, "toString");
401404

402405
const connected = connectOnce();
403406
socket.emitOpen();
@@ -413,14 +416,15 @@ describe("mattermost websocket monitor", () => {
413416
queueMicrotask(resolve);
414417
});
415418
expect(onPosted).not.toHaveBeenCalled();
416-
socket.emitMessage(Buffer.from(JSON.stringify(posted)));
419+
socket.emitMessage(frame);
417420
await vi.waitFor(() => {
418421
expect(onPosted).toHaveBeenCalledTimes(1);
419422
});
420423
socket.emitClose(1000);
421424
await connected;
422425

423-
expect(onPosted).toHaveBeenCalledWith(JSON.stringify(posted));
426+
expect(onPosted).toHaveBeenCalledWith(raw);
427+
expect(decode.mock.calls.length).toBeLessThanOrEqual(1);
424428
});
425429

426430
it("terminates when bot update_at changes (disable/enable cycle)", async () => {

‎extensions/mattermost/src/mattermost/monitor-websocket.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,15 @@ export function createMattermostConnectOnce(
336336
});
337337

338338
ws.on("message", async (data) => {
339+
const raw = rawDataToString(data);
339340
captureWsEvent({
340341
url: opts.wsUrl,
341342
direction: "inbound",
342343
kind: "ws-frame",
343344
flowId,
344-
payload: Buffer.from(rawDataToString(data)),
345+
payload: Buffer.from(raw),
345346
meta: { subsystem: "mattermost-websocket" },
346347
});
347-
const raw = rawDataToString(data);
348348
const payload = parseMattermostEventPayload(raw);
349349
if (!payload) {
350350
return;

0 commit comments

Comments
 (0)