From cd48d3dbde989d8b430ca13cd92be732c05b998b Mon Sep 17 00:00:00 2001 From: Leo Liang Date: Mon, 6 Jan 2020 15:00:08 +0800 Subject: [PATCH] Apply fix to #13: Client stays in SYNC_CONNECTED state when disconnected from the server --- lib/ConnectionManager.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/ConnectionManager.js b/lib/ConnectionManager.js index f402243..3d3a475 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(Buffer.alloc(0));