Skip to content

Commit

Permalink
Merge 18ec207 into 49acacf
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacgr committed Aug 22, 2021
2 parents 49acacf + 18ec207 commit bbdea52
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [Jaysonic - A persistent JSON-RPC client and server](#jaysonic---a-persistent-json-rpc-client-and-server)
- [List of features](#list-of-features)
- [Download & Installation](#download--installation)
Expand All @@ -19,10 +18,10 @@
- [Other client and server options](#other-client-and-server-options)
- [Code Demos](#code-demos)
- [Initialization](#initialization-1)
- [TCP](#tcp)
- [HTTP](#http)
- [HTTPS](#https)
- [Websocket](#websocket)
- [TCP](#tcp)
- [HTTP](#http)
- [HTTPS](#https)
- [Websocket](#websocket)
- [Server side](#server-side)
- [Instantiation and Listening](#instantiation-and-listening)
- [Closing the connection](#closing-the-connection)
Expand Down Expand Up @@ -206,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`. \
`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.\
`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
29 changes: 22 additions & 7 deletions src/client/protocol/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ class JsonRpcClientProtocol {
* Calls [listen]{@link JsonRpcClientProtocol#listen} if connection was successful, and will resolve the promise.
*
* Will retry connection on the `connectionTimeout` interval.
* Number of connection retries is based on `remainingRetries`
* 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.
*
* Will reject the promise if connect or re-connect attempts fail.
*
Expand All @@ -80,12 +83,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 All @@ -106,10 +118,13 @@ class JsonRpcClientProtocol {
*
* Sets `JsonRpcClientFactory.pcolInstance` to `undefined`
*
* Clears the connection timeout
*
* @param {function} cb Called when connection is sucessfully closed
*/
end(cb) {
this.factory.pcolInstance = undefined;
clearTimeout(this.connectionTimeout);
this.connector.end(cb);
}

Expand Down
18 changes: 17 additions & 1 deletion tests/connections/base-client-protocol.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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 });

describe("Base Client Reconnect", () => {
it("should retry the connection to the server and log the attempts", (done) => {
Expand All @@ -14,11 +15,26 @@ describe("Base Client Reconnect", () => {
setTimeout(() => {
unhook();
expect(capturedText).to.equal(
"Unable to connect. Retrying. 0 attempts left.\n"
"Failed to connect. Address [127.0.0.1:8100]. Retrying. 0 attempts left.\n"
);
}, 100);
conn.catch(() => {
done();
});
}).timeout(10000);
it("should retry the connection to the server indefinitely if retries set to 'Infinity'", (done) => {
infClient.connect();
let capturedText = "";
const unhook = intercept((text) => {
capturedText += text;
});
setTimeout(() => {
unhook();
expect(capturedText).to.equal(
"Failed to connect. Address [127.0.0.1:8100]. Retrying.\n"
);
infClient.end();
done();
}, 100);
}).timeout(10000);
});

0 comments on commit bbdea52

Please sign in to comment.