Skip to content

Commit

Permalink
Apply fix to alexguan#13: Client stays in SYNC_CONNECTED state when d…
Browse files Browse the repository at this point in the history
…isconnected from the server
  • Loading branch information
aleung committed Jan 6, 2020
1 parent 0a7cb95 commit cd48d3d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/ConnectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function ConnectionManager(connectionString, options, stateListener) {

this.updateTimeout(options.sessionTimeout);
this.connectTimeoutHandler = null;
this.readTimeoutHandler = null;

this.xid = 0;

Expand Down Expand Up @@ -113,7 +114,7 @@ ConnectionManager.prototype.updateTimeout = function (sessionTimeout) {
// We at least send out one ping one third of the session timeout, so
// the read timeout is two third of the session timeout.
this.pingTimeout = Math.floor(this.sessionTimeout / 3);
// this.readTimeout = Math.floor(sessionTimeout * 2 / 3);
this.readTimeout = Math.floor(sessionTimeout * 2 / 3);
};

/**
Expand Down Expand Up @@ -285,7 +286,10 @@ ConnectionManager.prototype.onSocketError = function (error) {
if (this.connectTimeoutHandler) {
clearTimeout(this.connectTimeoutHandler);
}

if (this.readTimeoutHandler) {
clearTimeout(this.readTimeoutHandler);
this.readTimeoutHandler = null;
}
// After socket error, the socket closed event will be triggered,
// we will retry connect in that listener function.
};
Expand Down Expand Up @@ -391,6 +395,12 @@ ConnectionManager.prototype.onSocketData = function (buffer) {
response,
event;

// clear readTimeoutHandler on receiving data
if (this.readTimeoutHandler) {
clearTimeout(this.readTimeoutHandler);
this.readTimeoutHandler = null;
}

// Combine the pending buffer with the new buffer.
if (self.pendingBuffer) {
buffer = Buffer.concat(
Expand Down Expand Up @@ -574,6 +584,13 @@ ConnectionManager.prototype.onSocketData = function (buffer) {
}
}

self.readTimeoutHandler = setTimeout(
function () {
self.socket.destroy(); // this will trigger reconnect
},
self.readTimeout
);

// We have more data to process, need to recursively process it.
if (self.pendingBuffer) {
self.onSocketData(Buffer.alloc(0));
Expand Down

0 comments on commit cd48d3d

Please sign in to comment.