From 53123ce7e8c347e02dd5e5406a4a83eade615e4f Mon Sep 17 00:00:00 2001 From: Natty Date: Sun, 17 Nov 2024 05:14:35 +0100 Subject: [PATCH] Fixed the RPC client singleton --- packages/backend/src/mag/rpc-client.ts | 24 ++++++++++++------- .../backend/src/queue/processors/deliver.ts | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/mag/rpc-client.ts b/packages/backend/src/mag/rpc-client.ts index 6e6ab0c762..ec999dc0c2 100644 --- a/packages/backend/src/mag/rpc-client.ts +++ b/packages/backend/src/mag/rpc-client.ts @@ -13,11 +13,21 @@ let serial: bigint = BigInt(0); const logger = new Logger("RpcLog"); async function getRpcClient(): Promise { - if (client != null) { - return client; - } - return new Promise((resolve) => { + if (client?.connecting) { + client!.once("connect", () => { + resolve(client!); + }); + return; + } + + if (client != null) { + resolve(client); + return; + } + + client = new Socket(); + const [host, portStr] = config.rpcHost.trim().split(/:(?=[0-9]+$)/, 2); const port = parseInt(portStr); @@ -30,7 +40,6 @@ async function getRpcClient(): Promise { client.on("connect", () => { exponentialBackoff = 0; - resolve(client!); }) client.on("error", e => { @@ -49,7 +58,6 @@ async function getRpcClient(): Promise { }); client.on("data", (recv) => { - logger.info(`data: ${recv}`); buf.writeBuffer(recv); if (buf.length < 1 + 4 + 8) { @@ -90,8 +98,8 @@ async function getRpcClient(): Promise { setTimeout(() => createClient(client!), 1000 * (1 + Math.pow(1.5, exponentialBackoff))); }; - client = new Socket(); - createClient(client); + createClient(client!); + resolve(client!); }); } diff --git a/packages/backend/src/queue/processors/deliver.ts b/packages/backend/src/queue/processors/deliver.ts index 5f71075aab..4021903045 100644 --- a/packages/backend/src/queue/processors/deliver.ts +++ b/packages/backend/src/queue/processors/deliver.ts @@ -75,7 +75,7 @@ export default async (job: Bull.Job) => { } // 5xx etc. - throw new Error(`${err.retry_class} ${err.kind}: ${err.message}`); + throw new Error(`${err.retry_class}: ${err.message}`); } else { throw res; }