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 Feb 11, 2019
1 parent b8ba391 commit 408495b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## unpublished

- Apply fix to [issue #13](https://github.com/alexguan/node-zookeeper-client/issues/13)
- Replace `underscore` with `lodash`

## 0.2.3
Expand Down
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(new Buffer(0));
Expand Down

0 comments on commit 408495b

Please sign in to comment.