Skip to content

Commit

Permalink
[api] Attempt reconnection on websocket errors (#65)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitryii Osipov <[email protected]>
  • Loading branch information
stefanmendoza-cb and osipov-mit authored Aug 21, 2023
1 parent 086d4a0 commit 3009c16
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions server/src/helpers/gear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,27 @@ export class GearApi {
runtime: this.runtime,
signedExtensions: this.signedExtensions,
});

this.api.on('connected', () => {
logger.info('Connected established!');
});

this.api.on('disconnected', () => {
logger.warn('Reconnection...');
logger.warn('Disconnected from node. Attempting to reconnect.');
this.connect();
});

this.api.on('error', (err: Error) => {
logger.error(
'An error occurred with the node connection. Sleeping for 60 seconds then attempting to reconnect.',
{ error: err }
);

const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
sleep(60000).then(this.connect);
});

this.genesis = this.api.genesisHash.toHex();
return this.api;
}

static async connect(config: NetworkConfig): Promise<GearApi> {
Expand Down

0 comments on commit 3009c16

Please sign in to comment.