Skip to content

Commit e15e15a

Browse files
committed
Avoid sending duplicate WebSocket failure messages or too often
We might as well limit this on the client side as well.
1 parent 43a14b6 commit e15e15a

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

ui/frontend/websocketMiddleware.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,37 @@ const WSMessageResponse = z.discriminatedUnion('type', [
1616
wsExecuteResponseSchema,
1717
]);
1818

19-
const reportWebSocketError = async (error: string) => {
20-
try {
21-
await fetch('/nowebsocket', {
22-
method: 'post',
23-
headers: {
24-
'Content-Type': 'application/json',
25-
},
26-
body: JSON.stringify({ error }),
27-
});
28-
} catch (reportError) {
29-
console.log('Unable to report WebSocket error', error, reportError);
30-
}
31-
};
19+
const reportWebSocketError = (() => {
20+
let lastReport: string | undefined;
21+
let lastReportTime = 0;
22+
23+
return async (error: string) => {
24+
// Don't worry about reporting the same thing again.
25+
if (lastReport === error) {
26+
return;
27+
}
28+
lastReport = error;
29+
30+
// Don't worry about spamming the server with reports.
31+
const now = Date.now();
32+
if (now - lastReportTime < 1000) {
33+
return;
34+
}
35+
lastReportTime = now;
36+
37+
try {
38+
await fetch('/nowebsocket', {
39+
method: 'post',
40+
headers: {
41+
'Content-Type': 'application/json',
42+
},
43+
body: JSON.stringify({ error }),
44+
});
45+
} catch (reportError) {
46+
console.log('Unable to report WebSocket error', error, reportError);
47+
}
48+
};
49+
})();
3250

3351
const openWebSocket = (currentLocation: Location) => {
3452
try {

0 commit comments

Comments
 (0)