Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fix to #13: Client stays in SYNC_CONNECTED state when disconnec… #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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