Skip to content

Commit

Permalink
fixed ConnectionManager infinite connect retries
Browse files Browse the repository at this point in the history
alexguan#37 PR in original repo
  • Loading branch information
artofspeed authored Oct 7, 2018
1 parent 290e468 commit b0f035e
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/ConnectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ function ConnectionManager(connectionString, options, stateListener) {
this.updateTimeout(options.sessionTimeout);
this.connectTimeoutHandler = null;

/**
* add sessionTimeoutHandler
*
* @type {number}
*/
this.sessionTimeoutHandler = null;

this.xid = 0;

this.sessionId = new Buffer(8);
Expand Down Expand Up @@ -218,6 +225,11 @@ ConnectionManager.prototype.connect = function () {

self.findNextServer(function (server) {
self.socket = net.connect(server);

self.sessionTimeoutHandler = setTimeout(
self.onSessionTimeout.bind(self),
self.options.sessionTimeout
);

self.connectTimeoutHandler = setTimeout(
self.onSocketConnectTimeout.bind(self),
Expand Down Expand Up @@ -290,6 +302,16 @@ ConnectionManager.prototype.onSocketError = function (error) {
// we will retry connect in that listener function.
};

ConnectionManager.prototype.onSessionTimeout = function () {
var connectStates = [STATES.CONNECTED, STATES.CONNECTED_READ_ONLY];
if (connectStates.indexOf(this.state) === -1) {
this.setState(STATES.SESSION_EXPIRED);
}
if (this.socket) {
this.socket.destroy();
}
};

ConnectionManager.prototype.onSocketConnectTimeout = function () {
// Destroy the current socket so the socket closed event
// will be trigger.
Expand All @@ -305,6 +327,8 @@ ConnectionManager.prototype.onSocketConnected = function () {

if (this.connectTimeoutHandler) {
clearTimeout(this.connectTimeoutHandler);
//clear sessionTimeoutHandler
clearTimeout(this.sessionTimeoutHandler);
}

connectRequest = new jute.Request(null, new jute.protocol.ConnectRequest(
Expand Down

0 comments on commit b0f035e

Please sign in to comment.