Skip to content

Commit

Permalink
feat: reconnect to lnd if streaming calls are disconnected #48
Browse files Browse the repository at this point in the history
  • Loading branch information
ImmanuelSegol committed Jan 8, 2019
1 parent 9b3c709 commit 10374f9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/lightning/LndClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ class LndClient extends BaseClient implements LightningClient {
return true;
}

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

this.logger.info(`Reestablished connection to ${LndClient.serviceName} ${this.symbol}`);
this.logger.info(`${LndClient.serviceName} ${this.symbol} status: ${response.syncedToChain}`);

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. */
public disconnect = () => {
this.clearReconnectTimer();
Expand Down Expand Up @@ -320,6 +340,8 @@ class LndClient extends BaseClient implements LightningClient {
.on('error', (error) => {
if (error.message !== '1 CANCELLED: Cancelled') {
this.logger.error(`Invoice subscription ended: ${error}`);
} else {
this.reconnect();
}
});
}
Expand Down

0 comments on commit 10374f9

Please sign in to comment.