diff --git a/CHANGELOG.md b/CHANGELOG.md index f256334..42baf3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/ConnectionManager.js b/lib/ConnectionManager.js index d3c0226..6ce0958 100644 --- a/lib/ConnectionManager.js +++ b/lib/ConnectionManager.js @@ -61,6 +61,7 @@ function ConnectionManager(connectionString, options, stateListener) { this.updateTimeout(options.sessionTimeout); this.connectTimeoutHandler = null; + this.readTimeoutHandler = null; this.xid = 0; @@ -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); }; /** @@ -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. }; @@ -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( @@ -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));