Skip to content

Commit

Permalink
fix: reconnect on rpc error (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
jowparks authored Apr 19, 2024
1 parent a79ab5f commit b966e8d
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/utils/ironfish.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IronfishSdk, RpcClient } from "@ironfish/sdk";
import { IronfishSdk, RpcClient, isRpcResponseError } from "@ironfish/sdk";
import { logger } from "./logger";

type ClientParams = {
Expand Down Expand Up @@ -28,8 +28,7 @@ class IronFishClient {
) {
const clientAddress = `${host}:${port}`;
const storedClient = this.clientRegistry.get(clientAddress);

if (storedClient) {
if (storedClient && (await this.connected(storedClient))) {
return storedClient;
}

Expand Down Expand Up @@ -62,6 +61,20 @@ class IronFishClient {

return client;
}

async connected(client: RpcClient | undefined): Promise<boolean> {
try {
const response = await client?.node.getStatus();
if (response && !isRpcResponseError(response)) {
return true;
}
} catch (error) {
logger.error(
`Error while getting status from IronFish RPC. Reconnecting...`,
);
}
return false;
}
}

export const ifClient = new IronFishClient();

0 comments on commit b966e8d

Please sign in to comment.