Skip to content

Commit ba8c8b3

Browse files
committed
fix(gateway): share websocket readiness without eager imports
1 parent 56c28f1 commit ba8c8b3

5 files changed

Lines changed: 9 additions & 8 deletions

File tree

‎src/gateway/node-registry.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import {
4747
type PendingSystemRunEvent,
4848
} from "./node-registry.invoke-stream.js";
4949
import { normalizeNodeSkillDescriptors } from "./node-skill-descriptors.js";
50-
import { MAX_BUFFERED_BYTES } from "./server-constants.js";
50+
import { MAX_BUFFERED_BYTES, WEBSOCKET_OPEN_READY_STATE } from "./server-constants.js";
5151
import type { GatewayWsClient } from "./server/ws-types.js";
5252

5353
/** Connected node session advertised over Gateway websocket. */
@@ -135,7 +135,6 @@ type PingableSocket = {
135135

136136
const SERIALIZED_EVENT_PAYLOAD = Symbol("openclaw.serializedEventPayload");
137137
const AUTHORIZED_SYSTEM_RUN_EVENT_GRACE_MS = 5 * 60 * 1000;
138-
const WEBSOCKET_OPEN_READY_STATE = 1;
139138
const SLOW_CONSUMER_CLOSE_CODE = 1008;
140139
const FAILED_EVENT_LOG_INTERVAL_MS = 30_000;
141140
const log = createSubsystemLogger("gateway/nodes");

‎src/gateway/server-broadcast.serialization.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// cause a synchronized reconnect storm) and must leave a server-side record.
44
import { once } from "node:events";
55
import type { AddressInfo } from "node:net";
6+
import { rawDataToString } from "@openclaw/gateway-client/websocket-data";
67
import { afterEach, describe, expect, it, vi } from "vitest";
78
import { WebSocket, WebSocketServer, type RawData } from "ws";
89
import { setVerbose } from "../global-state.js";
@@ -100,7 +101,7 @@ describe("broadcast serialization failures", () => {
100101
const healthy = await connectPeer();
101102
const delivered: Array<{ event: string; seq: number }> = [];
102103
healthy.peer.on("message", (data: RawData) => {
103-
delivered.push(JSON.parse(data.toString()) as { event: string; seq: number });
104+
delivered.push(JSON.parse(rawDataToString(data)) as { event: string; seq: number });
104105
});
105106
const makeRealClient = (connId: string, socket: WebSocket): GatewayWsClient => ({
106107
connId,

‎src/gateway/server-broadcast.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { WebSocket } from "ws";
21
import {
32
GATEWAY_CLIENT_CAPS,
43
hasGatewayClientCap,
@@ -32,7 +31,7 @@ import type {
3231
GatewayPluginEventScope,
3332
} from "./server-broadcast-types.js";
3433
import type { SessionMessageSubscriberRegistry } from "./server-chat-state.js";
35-
import { MAX_BUFFERED_BYTES } from "./server-constants.js";
34+
import { MAX_BUFFERED_BYTES, WEBSOCKET_OPEN_READY_STATE } from "./server-constants.js";
3635
import type { GatewayWsClient } from "./server/ws-types.js";
3736
import { logWs, summarizeAgentEventForWsLog } from "./ws-log.js";
3837

@@ -271,7 +270,7 @@ export function createGatewayBroadcaster(params: {
271270
let sessionSubscriberConnIdsByKey: Array<ReadonlySet<string> | undefined> | undefined;
272271
for (const c of params.clients) {
273272
// Closing nodes remain discoverable until their owner drains admitted lifecycle work.
274-
if (c.invalidated === true || c.socket.readyState !== WebSocket.OPEN) {
273+
if (c.invalidated === true || c.socket.readyState !== WEBSOCKET_OPEN_READY_STATE) {
275274
continue;
276275
}
277276
if (targetConnIds && !targetConnIds.has(c.connId)) {

‎src/gateway/server-constants.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
export const MAX_PAYLOAD_BYTES = 25 * 1024 * 1024;
44
export const MAX_BUFFERED_BYTES = 50 * 1024 * 1024; // per-connection send buffer limit (2x max payload)
55
export const MAX_PREAUTH_PAYLOAD_BYTES = 64 * 1024;
6+
export const WEBSOCKET_OPEN_READY_STATE = 1;
67

78
const DEFAULT_MAX_CHAT_HISTORY_MESSAGES_BYTES = 6 * 1024 * 1024; // keep history responses comfortably under client WS limits
89
const maxChatHistoryMessagesBytes = DEFAULT_MAX_CHAT_HISTORY_MESSAGES_BYTES;

‎src/gateway/server/ws-connection.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Gateway WebSocket connection handler owns pre-auth limits, handshake auth, presence, and message-handler attachment.
22
import { randomUUID } from "node:crypto";
33
import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce";
4-
import { WebSocket, type RawData, type WebSocketServer } from "ws";
4+
import type { RawData, WebSocket, WebSocketServer } from "ws";
55
import { WORKER_PROTOCOL_MAX_PAYLOAD_BYTES } from "../../../packages/gateway-protocol/src/index.js";
66
import { GATEWAY_STARTUP_PENDING_CLOSE_CAUSE } from "../../../packages/gateway-protocol/src/startup-unavailable.js";
77
import { getRuntimeConfig } from "../../config/io.js";
@@ -25,6 +25,7 @@ import {
2525
MAX_BUFFERED_BYTES,
2626
MAX_PAYLOAD_BYTES,
2727
MAX_PREAUTH_PAYLOAD_BYTES,
28+
WEBSOCKET_OPEN_READY_STATE,
2829
} from "../server-constants.js";
2930
import type { GatewayRequestContext, GatewayRequestHandlers } from "../server-methods/types.js";
3031
import { formatError } from "../server-utils.js";
@@ -349,7 +350,7 @@ export function attachGatewayWsConnectionHandler(params: AttachGatewayWsConnecti
349350
if (closed) {
350351
return { kind: "unavailable" } as const;
351352
}
352-
if (socket.readyState !== WebSocket.OPEN) {
353+
if (socket.readyState !== WEBSOCKET_OPEN_READY_STATE) {
353354
// Keep pending node results revocable until their close handler drains admitted work.
354355
if (client?.connect.role === "node" && nodeLifecycleDispatch.hasActive()) {
355356
retainClientUntilNodeDrain = true;

0 commit comments

Comments
 (0)