Fixed the RPC client singleton
ci/woodpecker/tag/ociImageTag Pipeline was successful Details

This commit is contained in:
Natty 2024-11-17 05:14:35 +01:00
parent 367257c3ad
commit 53123ce7e8
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
2 changed files with 17 additions and 9 deletions

View File

@ -13,11 +13,21 @@ let serial: bigint = BigInt(0);
const logger = new Logger("RpcLog"); const logger = new Logger("RpcLog");
async function getRpcClient(): Promise<Socket> { async function getRpcClient(): Promise<Socket> {
if (client != null) {
return client;
}
return new Promise((resolve) => { 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 [host, portStr] = config.rpcHost.trim().split(/:(?=[0-9]+$)/, 2);
const port = parseInt(portStr); const port = parseInt(portStr);
@ -30,7 +40,6 @@ async function getRpcClient(): Promise<Socket> {
client.on("connect", () => { client.on("connect", () => {
exponentialBackoff = 0; exponentialBackoff = 0;
resolve(client!);
}) })
client.on("error", e => { client.on("error", e => {
@ -49,7 +58,6 @@ async function getRpcClient(): Promise<Socket> {
}); });
client.on("data", (recv) => { client.on("data", (recv) => {
logger.info(`data: ${recv}`);
buf.writeBuffer(recv); buf.writeBuffer(recv);
if (buf.length < 1 + 4 + 8) { if (buf.length < 1 + 4 + 8) {
@ -90,8 +98,8 @@ async function getRpcClient(): Promise<Socket> {
setTimeout(() => createClient(client!), 1000 * (1 + Math.pow(1.5, exponentialBackoff))); setTimeout(() => createClient(client!), 1000 * (1 + Math.pow(1.5, exponentialBackoff)));
}; };
client = new Socket(); createClient(client!);
createClient(client); resolve(client!);
}); });
} }

View File

@ -75,7 +75,7 @@ export default async (job: Bull.Job<DeliverJobData>) => {
} }
// 5xx etc. // 5xx etc.
throw new Error(`${err.retry_class} ${err.kind}: ${err.message}`); throw new Error(`${err.retry_class}: ${err.message}`);
} else { } else {
throw res; throw res;
} }