Retrying connecting host with extending delays. (#144) (#147)

This commit is contained in:
Kithmini Gunawardhana
2022-06-29 17:38:14 +05:30
committed by GitHub
parent 03166b7cc9
commit 01fbdc3c6f

View File

@@ -47,8 +47,7 @@ class MessageBoard {
await this.xrplApi.connect();
this.hostClient = new evernode.HostClient(this.cfg.xrpl.address, this.cfg.xrpl.secret);
await this.hostClient.connect();
await this.#connectHost();
// Get last heartbeat moment from the host info.
const hostInfo = await this.hostClient.getRegistration();
if (!hostInfo)
@@ -137,6 +136,34 @@ class MessageBoard {
this.hostClient.on(evernode.HostEvents.ExtendLease, r => this.handleExtendLease(r));
}
// Connect the host and trying to reconnect in the event of account not found error.
// Account not found error can be because of a network reset. (Dev and test nets)
async #connectHost() {
let attempts = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
try {
attempts++;
const ret = await this.hostClient.connect();
if (ret)
break;
} catch (error) {
if (error?.data?.error === 'actNotFound') {
let delaySec;
// The maximum delay will be 5 minutes.
if (attempts > 150) {
delaySec = 300;
} else {
delaySec = 2 * attempts;
}
console.log(`Network reset detected. Attempt ${attempts} failed. Retrying in ${delaySec}s...`);
await new Promise(resolve => setTimeout(resolve, delaySec * 1000));
} else
throw error;
}
}
}
async recreateLeaseOffer(nfTokenId, tenantAddress, leaseIndex) {
// Burn the NFTs and recreate the offer and send back the lease amount back to the tenant.
await this.hostClient.expireLease(nfTokenId, tenantAddress).catch(console.error);