Skip to content

Commit

Permalink
feat: reconnect to lnd if streaming calls are disconnected #48 (#59)
Browse files Browse the repository at this point in the history
* feat: reconnect to lnd if streaming calls are disconnected
  • Loading branch information
ImmanuelSegol authored Feb 5, 2019
1 parent a7291a8 commit 7e524f5
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/lightning/LndClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,26 @@ class LndClient extends BaseClient implements LightningClient {
return true;
}

private reconnect = async () => {
try {
await this.getInfo();

this.logger.info(`Reestablished connection to ${LndClient.serviceName} ${this.symbol}`);
this.setClientStatus(ClientStatus.Connected);
this.clearReconnectTimer();
this.subscribeInvoices();

} catch (err) {
this.logger.error(`Could not reconnect to ${LndClient.serviceName} ${this.symbol}: ${err}`);
this.logger.info(`Retrying in ${this.RECONNECT_INTERVAL} ms`);

this.setClientStatus(ClientStatus.Disconnected);
this.reconnectionTimer = setTimeout(this.reconnect, this.RECONNECT_INTERVAL);
}
}

/**
* End all subscriptions and reconnection attempts
* End all subscriptions and reconnection attempts.
*/
public disconnect = () => {
this.clearReconnectTimer();
Expand Down Expand Up @@ -344,10 +362,11 @@ class LndClient extends BaseClient implements LightningClient {
this.emit('invoice.settled', paymentReq, invoice.getRPreimage_asB64());
}
})
.on('error', (error) => {
.on('error', async (error) => {
if (error.message !== '1 CANCELLED: Cancelled') {
this.logger.error(`Invoice subscription ended: ${error}`);
}
await this.reconnect();
});
}
}
Expand Down

0 comments on commit 7e524f5

Please sign in to comment.