Skip to content

Commit

Permalink
Retry connection to server indefinitely if retries is set to Infinity…
Browse files Browse the repository at this point in the history
…. Refs #94.
  • Loading branch information
isaacgr committed Aug 22, 2021
1 parent b255842 commit 897ba53
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/client/protocol/base.js
Expand Up @@ -81,12 +81,21 @@ class JsonRpcClientProtocol {
resolve(this.server);
});
this.connector.on("error", (error) => {
if (error.code === "ECONNREFUSED" && this.factory.remainingRetries) {
this.factory.remainingRetries -= 1;
console.error(
`Unable to connect. Retrying. ${this.factory.remainingRetries} attempts left.`
);
setTimeout(() => {
if (
error.code === "ECONNREFUSED"
&& this.factory.remainingRetries > 0
) {
if (Number.isFinite(this.factory.remainingRetries)) {
this.factory.remainingRetries -= 1;
console.error(
`Failed to connect. Address [${this.server.host}:${this.server.port}]. Retrying. ${this.factory.remainingRetries} attempts left.`
);
} else {
console.error(
`Failed to connect. Address [${this.server.host}:${this.server.port}]. Retrying.`
);
}
this.connectionTimeout = setTimeout(() => {
retryConnection();
}, this.factory.connectionTimeout);
} else {
Expand Down

0 comments on commit 897ba53

Please sign in to comment.