mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-22 09:01:46 +03:00
perf: reduce gateway multi e2e websocket churn
This commit is contained in:
@@ -302,15 +302,15 @@ const connectNode = async (
|
|||||||
return { client, nodeId };
|
return { client, nodeId };
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchNodeList = async (
|
const connectStatusClient = async (
|
||||||
inst: GatewayInstance,
|
inst: GatewayInstance,
|
||||||
timeoutMs = 5_000,
|
timeoutMs = 5_000,
|
||||||
): Promise<NodeListPayload> => {
|
): Promise<GatewayClient> => {
|
||||||
let settled = false;
|
let settled = false;
|
||||||
let timer: NodeJS.Timeout | null = null;
|
let timer: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
return await new Promise<NodeListPayload>((resolve, reject) => {
|
return await new Promise<GatewayClient>((resolve, reject) => {
|
||||||
const finish = (err?: Error, payload?: NodeListPayload) => {
|
const finish = (err?: Error) => {
|
||||||
if (settled) {
|
if (settled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -318,12 +318,11 @@ const fetchNodeList = async (
|
|||||||
if (timer) {
|
if (timer) {
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
}
|
}
|
||||||
client.stop();
|
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
resolve(payload ?? {});
|
resolve(client);
|
||||||
};
|
};
|
||||||
|
|
||||||
const client = new GatewayClient({
|
const client = new GatewayClient({
|
||||||
@@ -335,10 +334,7 @@ const fetchNodeList = async (
|
|||||||
platform: "test",
|
platform: "test",
|
||||||
mode: GATEWAY_CLIENT_MODES.CLI,
|
mode: GATEWAY_CLIENT_MODES.CLI,
|
||||||
onHelloOk: () => {
|
onHelloOk: () => {
|
||||||
void client
|
finish();
|
||||||
.request<NodeListPayload>("node.list", {})
|
|
||||||
.then((payload) => finish(undefined, payload))
|
|
||||||
.catch((err) => finish(err instanceof Error ? err : new Error(String(err))));
|
|
||||||
},
|
},
|
||||||
onConnectError: (err) => finish(err),
|
onConnectError: (err) => finish(err),
|
||||||
onClose: (code, reason) => {
|
onClose: (code, reason) => {
|
||||||
@@ -356,13 +352,18 @@ const fetchNodeList = async (
|
|||||||
|
|
||||||
const waitForNodeStatus = async (inst: GatewayInstance, nodeId: string, timeoutMs = 10_000) => {
|
const waitForNodeStatus = async (inst: GatewayInstance, nodeId: string, timeoutMs = 10_000) => {
|
||||||
const deadline = Date.now() + timeoutMs;
|
const deadline = Date.now() + timeoutMs;
|
||||||
while (Date.now() < deadline) {
|
const client = await connectStatusClient(inst);
|
||||||
const list = await fetchNodeList(inst);
|
try {
|
||||||
const match = list.nodes?.find((n) => n.nodeId === nodeId);
|
while (Date.now() < deadline) {
|
||||||
if (match?.connected && match?.paired) {
|
const list = await client.request<NodeListPayload>("node.list", {});
|
||||||
return;
|
const match = list.nodes?.find((n) => n.nodeId === nodeId);
|
||||||
|
if (match?.connected && match?.paired) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await sleep(50);
|
||||||
}
|
}
|
||||||
await sleep(50);
|
} finally {
|
||||||
|
client.stop();
|
||||||
}
|
}
|
||||||
throw new Error(`timeout waiting for node status for ${nodeId}`);
|
throw new Error(`timeout waiting for node status for ${nodeId}`);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user