fix(gateway): wait for event loop before client start

This commit is contained in:
Peter Steinberger
2026-04-29 14:50:21 +01:00
parent a972c9ec45
commit 6bbacd14a3
24 changed files with 675 additions and 68 deletions

View File

@@ -1,5 +1,8 @@
import { setTimeout as sleep } from "node:timers/promises";
import { GatewayClient } from "openclaw/plugin-sdk/gateway-runtime";
import {
GatewayClient,
startGatewayClientWhenEventLoopReady,
} from "openclaw/plugin-sdk/gateway-runtime";
import type { GoogleMeetConfig } from "./config.js";
type VoiceCallGatewayClient = InstanceType<typeof GatewayClient>;
@@ -20,10 +23,11 @@ async function createConnectedGatewayClient(
): Promise<VoiceCallGatewayClient> {
let client: VoiceCallGatewayClient;
await new Promise<void>((resolve, reject) => {
const timer = setTimeout(
() => reject(new Error("gateway connect timeout")),
config.voiceCall.requestTimeoutMs,
);
const abortStart = new AbortController();
const timer = setTimeout(() => {
abortStart.abort();
reject(new Error("gateway connect timeout"));
}, config.voiceCall.requestTimeoutMs);
client = new GatewayClient({
url: config.voiceCall.gatewayUrl,
token: config.voiceCall.token,
@@ -37,10 +41,24 @@ async function createConnectedGatewayClient(
},
onConnectError: (err) => {
clearTimeout(timer);
abortStart.abort();
reject(err);
},
});
client.start();
void startGatewayClientWhenEventLoopReady(client, {
timeoutMs: config.voiceCall.requestTimeoutMs,
signal: abortStart.signal,
})
.then((readiness) => {
if (!readiness.ready && !readiness.aborted) {
clearTimeout(timer);
reject(new Error("gateway event loop readiness timeout"));
}
})
.catch((err) => {
clearTimeout(timer);
reject(err instanceof Error ? err : new Error(String(err)));
});
});
return client!;
}