Skip to content

Commit

Permalink
add support for retry limit
Browse files Browse the repository at this point in the history
  • Loading branch information
t2013anurag committed Jan 21, 2019
1 parent 65cbe1c commit bd8abf5
Show file tree
Hide file tree
Showing 3 changed files with 1,742 additions and 4 deletions.
26 changes: 23 additions & 3 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var SockJS;
* @param {String} url
* @constructor
*/
function Client(url) {
function Client(url, retryAttempts) {
/** @type {String} */
this._url = url;
/** @type {SockJS} */
Expand All @@ -19,8 +19,18 @@ function Client(url) {
this._heartbeatTimeout = null;
/** @type {Number} */
this._reopenTimeout = null;
/** @type {Number} */
this._retryAttempts = retryAttempts || 6;
/** @type {Number} */
this._counter = 0;
/** @type {Function} */
this._errorCallback = null;
}

Client.prototype.onCaptureError = function(callback) {
this._errorCallback = callback;
};

Client.prototype.open = function() {
clearTimeout(this._reopenTimeout);
this._reopenTimeout = null;
Expand All @@ -39,8 +49,15 @@ Client.prototype._reopen = function() {
this._sockJS = null;

this._reopenTimeout = setTimeout(function() {
this.open();
}.bind(this), 1000);
if (this._counter < this._retryAttempts) {
this._counter++;
this.open();
} else {
if (typeof this._errorCallback === 'function') {
this._errorCallback(new Error('Reached max retryAttempts'));
}
}
}.bind(this), 1000 * this._counter);
};

Client.prototype._onopen = function() {
Expand All @@ -49,6 +66,9 @@ Client.prototype._onopen = function() {
self._subscribe(channel, self._closeStamp);
});

/** Reset counter if connected. */
this._counter = 0;

this._closeStamp = null;
this._sockJS.onmessage = function(event) {
var data = JSON.parse(event.data);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "socket-redis",
"version": "3.3.0",
"version": "3.3.4",
"description": "Redis to SockJS relay",
"main": "socket-redis.js",
"bin": {
Expand Down
Loading

0 comments on commit bd8abf5

Please sign in to comment.