mirror of
https://github.com/block/buzz.git
synced 2026-11-27 03:31:50 +00:00
fix(desktop): fast-track relay restart reconnects (#2579)
Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@buzz.block.builderlab.xyz> Signed-off-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@buzz.block.builderlab.xyz> Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@buzz.block.builderlab.xyz> Co-authored-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@buzz.block.builderlab.xyz>
This commit is contained in:
@@ -43,6 +43,8 @@ import {
|
||||
import { requestHistoryGated } from "@/shared/api/relayGateBoundary";
|
||||
import { RelayConnectionStateEmitter } from "@/shared/api/relayConnectionStateEmitter";
|
||||
import {
|
||||
isServiceRestartClose,
|
||||
isWebSocketClose,
|
||||
shouldRefuseConnect,
|
||||
shouldScheduleReconnect,
|
||||
} from "@/shared/api/relayReconnectPolicy";
|
||||
@@ -767,16 +769,12 @@ export class RelayClient {
|
||||
if (generation !== this.connectionGeneration) return;
|
||||
this.stallWatchdog.recordInbound();
|
||||
|
||||
if (
|
||||
typeof message === "object" &&
|
||||
message !== null &&
|
||||
"type" in message &&
|
||||
message.type === "Close"
|
||||
) {
|
||||
if (isWebSocketClose(message)) {
|
||||
if (isServiceRestartClose(message))
|
||||
this.reconnectDelayMs = RECONNECT_BASE_DELAY_MS;
|
||||
this.resetConnection(new Error("Relay connection closed."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof message === "object" &&
|
||||
message !== null &&
|
||||
|
||||
@@ -2,6 +2,8 @@ import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
import {
|
||||
isServiceRestartClose,
|
||||
isWebSocketClose,
|
||||
shouldRefuseConnect,
|
||||
shouldScheduleReconnect,
|
||||
} from "./relayReconnectPolicy.ts";
|
||||
@@ -81,3 +83,25 @@ test("shouldRefuseConnect mirrors terminal", () => {
|
||||
assert.equal(shouldRefuseConnect({ terminal: false }), false);
|
||||
assert.equal(shouldRefuseConnect({ terminal: true }), true);
|
||||
});
|
||||
|
||||
test("only a close frame with code 1012 is a service restart", () => {
|
||||
assert.equal(isWebSocketClose({ type: "Close" }), true);
|
||||
assert.equal(isWebSocketClose({ type: "Error" }), false);
|
||||
assert.equal(
|
||||
isServiceRestartClose({
|
||||
type: "Close",
|
||||
data: { code: 1012, reason: "relay restarting" },
|
||||
}),
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
isServiceRestartClose({ type: "Close", data: { code: 1000 } }),
|
||||
false,
|
||||
);
|
||||
assert.equal(isServiceRestartClose({ type: "Close" }), false);
|
||||
assert.equal(
|
||||
isServiceRestartClose({ type: "Error", data: { code: 1012 } }),
|
||||
false,
|
||||
);
|
||||
assert.equal(isServiceRestartClose(null), false);
|
||||
});
|
||||
|
||||
@@ -41,3 +41,26 @@ export function shouldScheduleReconnect(inputs: RelayReconnectInputs): boolean {
|
||||
export function shouldRefuseConnect(inputs: { terminal: boolean }): boolean {
|
||||
return inputs.terminal;
|
||||
}
|
||||
|
||||
export function isWebSocketClose(
|
||||
message: unknown,
|
||||
): message is { type: "Close"; data?: unknown } {
|
||||
return (
|
||||
typeof message === "object" &&
|
||||
message !== null &&
|
||||
"type" in message &&
|
||||
message.type === "Close"
|
||||
);
|
||||
}
|
||||
|
||||
export function isServiceRestartClose(message: unknown): boolean {
|
||||
if (!isWebSocketClose(message)) return false;
|
||||
if (!("data" in message)) return false;
|
||||
const data = message.data;
|
||||
return (
|
||||
typeof data === "object" &&
|
||||
data !== null &&
|
||||
"code" in data &&
|
||||
data.code === 1012
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1038,6 +1038,7 @@ declare global {
|
||||
__BUZZ_E2E_GET_RELAY_CONNECTION_STATE__?: () => ConnectionState;
|
||||
__BUZZ_E2E_SET_STALL_WEBSOCKET_SENDS__?: (stall: boolean) => void;
|
||||
__BUZZ_E2E_DISCONNECT_MOCK_WEBSOCKETS__?: () => number;
|
||||
__BUZZ_E2E_RESTART_MOCK_WEBSOCKETS__?: () => number;
|
||||
__BUZZ_E2E_SET_MESH__?: (mesh: {
|
||||
admitted?: boolean;
|
||||
models?: Array<{ id: string; name: string | null }>;
|
||||
@@ -3326,9 +3327,10 @@ function sendWsText(handler: WsHandler, payload: unknown[]) {
|
||||
});
|
||||
}
|
||||
|
||||
function sendWsClose(handler: WsHandler) {
|
||||
function sendWsClose(handler: WsHandler, code?: number, reason?: string) {
|
||||
handler({
|
||||
type: "Close",
|
||||
data: code === undefined ? undefined : { code, reason: reason ?? "" },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9085,6 +9087,14 @@ export function maybeInstallE2eTauriMocks() {
|
||||
for (const socketId of socketIds) disconnectMockSocket(socketId);
|
||||
return socketIds.length;
|
||||
};
|
||||
window.__BUZZ_E2E_RESTART_MOCK_WEBSOCKETS__ = () => {
|
||||
const sockets = [...mockSockets.values()];
|
||||
mockSockets.clear();
|
||||
for (const socket of sockets) {
|
||||
sendWsClose(socket.handler, 1012, "relay restarting");
|
||||
}
|
||||
return sockets.length;
|
||||
};
|
||||
// Tests vary mesh admission and models to exercise provider discovery and
|
||||
// the managed-agent start preflight.
|
||||
window.__BUZZ_E2E_SET_MESH__ = (mesh) => {
|
||||
|
||||
@@ -35,6 +35,20 @@ async function disconnectMockWebsockets(page: import("@playwright/test").Page) {
|
||||
expect(disconnected).toBeGreaterThan(0);
|
||||
}
|
||||
|
||||
async function restartMockWebsockets(page: import("@playwright/test").Page) {
|
||||
const restarted = await page.evaluate(() => {
|
||||
const restart = (
|
||||
window as Window & {
|
||||
__BUZZ_E2E_RESTART_MOCK_WEBSOCKETS__?: () => number;
|
||||
}
|
||||
).__BUZZ_E2E_RESTART_MOCK_WEBSOCKETS__;
|
||||
if (!restart)
|
||||
throw new Error("E2E websocket restart seam is not installed.");
|
||||
return restart();
|
||||
});
|
||||
expect(restarted).toBeGreaterThan(0);
|
||||
}
|
||||
|
||||
async function emitMockMessages(
|
||||
page: import("@playwright/test").Page,
|
||||
messages: Array<{ content: string; createdAt: number }>,
|
||||
@@ -107,6 +121,27 @@ test("failed initial relay dial retries automatically", async ({ page }) => {
|
||||
await expect(page.getByTestId("channel-general")).toBeVisible();
|
||||
});
|
||||
|
||||
test("service restart close resets accumulated backoff", async ({ page }) => {
|
||||
await installMockBridge(page, {
|
||||
websocketConnectErrors: ["down 1", "down 2", "down 3"],
|
||||
});
|
||||
await page.goto("/");
|
||||
await expect(page.getByTestId("channel-general")).toBeVisible({
|
||||
timeout: 15_000,
|
||||
});
|
||||
|
||||
const startedAt = Date.now();
|
||||
await restartMockWebsockets(page);
|
||||
await expect
|
||||
.poll(
|
||||
() =>
|
||||
page.evaluate(() => window.__BUZZ_E2E_GET_RELAY_CONNECTION_STATE__?.()),
|
||||
{ timeout: 2_500 },
|
||||
)
|
||||
.toBe("connected");
|
||||
expect(Date.now() - startedAt).toBeLessThan(2_500);
|
||||
});
|
||||
|
||||
test("passive relay watchdog does not write while the websocket is half-open", async ({
|
||||
page,
|
||||
}) => {
|
||||
|
||||
Reference in New Issue
Block a user