Skip to content

Commit

Permalink
Use 'null' to establish indefinite retries. Refs #94.
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacgr committed Aug 23, 2021
1 parent 37bf969 commit 83a2080
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -205,7 +205,7 @@ The client and server support changing the JSON-RPC version and the delimiter us

#### Client only options

`retries`: The number of retry attempts for the client to connect to the server. Default is `2`. If set to `Infinity` (or some number that Javascript defines as non-finite `https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite`) then the client will retry indefinitely.\
`retries`: The number of retry attempts for the client to connect to the server. Default is `2`. If set to `null` then the client will retry indefinitely.\
`timeout`: The amount of time before a request times out. Will return a `-32000` error code. The default value is `30` (in seconds). \
`connectionTimeout`: The amount of time between connection retry attempts to a server. The default value is `5000` (in milliseconds).

Expand Down
22 changes: 8 additions & 14 deletions src/client/protocol/base.js
Expand Up @@ -63,17 +63,15 @@ class JsonRpcClientProtocol {
* Will retry connection on the `connectionTimeout` interval.
* Number of connection retries is based on `remainingRetries`.
*
* If `Infinity` (or some number that Javascript defines as non-finite `https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite`)
* is set for number of retries, then connections will attempt indefinitely.
* If `null` is set for number of retries, then connections will attempt indefinitely.
*
* Will reject the promise if connect or re-connect attempts fail and there are no remaining retries.
*
* @returns Promise
*
*
*/
connect() {
return new Promise(this._retryConnection.bind(this)); // .bind so I dont have to redo linting rules around line length
return new Promise((resolve, reject) => this._retryConnection(resolve, reject));
}

/**
Expand All @@ -96,14 +94,7 @@ class JsonRpcClientProtocol {
this.listen();
resolve(this.server);
});
this.connector.on("error", (error) => {
if (error.code === "ECONNREFUSED" && this.factory.remainingRetries > 0) {
this._onConnectionFailed(resolve, reject);
} else {
this.factory.pcolInstance = undefined;
reject(error);
}
});
this.connector.on("error", error => this._onConnectionFailed(error, resolve, reject));
this.connector.on("close", () => {
this.factory.emit("serverDisconnected");
});
Expand All @@ -123,12 +114,15 @@ class JsonRpcClientProtocol {
*
* @private
*/
_onConnectionFailed(resolve, reject) {
if (Number.isFinite(this.factory.remainingRetries)) {
_onConnectionFailed(error, resolve, reject) {
if (this.factory.remainingRetries > 0) {
this.factory.remainingRetries -= 1;
console.error(
`Failed to connect. Address [${this.server.host}:${this.server.port}]. Retrying. ${this.factory.remainingRetries} attempts left.`
);
} else if (this.factory.remainingRetries === 0) {
this.factory.pcolInstance = undefined;
return reject(error);
} else {
console.error(
`Failed to connect. Address [${this.server.host}:${this.server.port}]. Retrying.`
Expand Down
12 changes: 6 additions & 6 deletions src/client/protocol/ws.js
Expand Up @@ -49,24 +49,24 @@ class WsClientProtocol extends JsonRpcClientProtocol {
console.log(
`Client closed connection. Code [${event.code}]. Reason [${event.reason}].`
);
} else if (this.factory.remainingRetries > 0) {
this._onConnectionFailed(resolve, reject);
} else {
this.factory.pcolInstance = undefined;
reject(event);
return this._onConnectionFailed(event, resolve, reject);
}
};
}

/**
* @inheritdoc
*/
_onConnectionFailed(resolve, reject) {
if (Number.isFinite(this.factory.remainingRetries)) {
_onConnectionFailed(event, resolve, reject) {
if (this.factory.remainingRetries > 0) {
this.factory.remainingRetries -= 1;
console.error(
`Failed to connect. Address [${this.url}]. Retrying. ${this.factory.remainingRetries} attempts left.`
);
} else if (this.factory.remainingRetries === 0) {
this.factory.pcolInstance = undefined;
return reject(event);
} else {
console.error(`Failed to connect. Address [${this.url}]. Retrying.`);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/connections/base-client-protocol.test.js
Expand Up @@ -3,7 +3,7 @@ const intercept = require("intercept-stdout");
const Jaysonic = require("../../src");

const client = new Jaysonic.client.tcp({ retries: 1 });
const infClient = new Jaysonic.client.tcp({ retries: Infinity });
const infClient = new Jaysonic.client.tcp({ retries: null });

describe("Base Client Reconnect", () => {
it("should retry the connection to the server and log the attempts", (done) => {
Expand All @@ -22,7 +22,7 @@ describe("Base Client Reconnect", () => {
done();
});
}).timeout(10000);
it("should retry the connection to the server indefinitely if retries set to 'Infinity'", (done) => {
it("should retry the connection to the server indefinitely if retries set to 'null'", (done) => {
infClient.connect();
let capturedText = "";
const unhook = intercept((text) => {
Expand Down
4 changes: 2 additions & 2 deletions tests/connections/ws-client-protocol.test.js
Expand Up @@ -4,7 +4,7 @@ const Jaysonic = require("../../src");
const { wss } = require("../test-server");

const wsClient = new Jaysonic.client.ws({ retries: 1 });
const infClient = new Jaysonic.client.ws({ retries: Infinity });
const infClient = new Jaysonic.client.ws({ retries: null });

describe("WS Client Reconnect", () => {
it("should attempt to reconnect to the server and reject promise if unable", (done) => {
Expand All @@ -14,7 +14,7 @@ describe("WS Client Reconnect", () => {
done();
});
}).timeout(10000);
it("should retry the connection to the server indefinitely if retries set to 'Infinity'", (done) => {
it("should retry the connection to the server indefinitely if retries set to 'null'", (done) => {
infClient.connect();
let capturedText = "";
const unhook = intercept((text) => {
Expand Down

0 comments on commit 83a2080

Please sign in to comment.