From e2e08cec266f63a6ca5b080280ea7dcbc1fbeb16 Mon Sep 17 00:00:00 2001 From: hufeng Date: Fri, 17 Apr 2015 23:44:00 +0800 Subject: [PATCH 1/7] fixed ConnectionManager infinite connect retries #30 --- lib/ConnectionManager.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/ConnectionManager.js b/lib/ConnectionManager.js index 0ae6086..8cf3ace 100644 --- a/lib/ConnectionManager.js +++ b/lib/ConnectionManager.js @@ -62,6 +62,13 @@ function ConnectionManager(connectionString, options, stateListener) { this.updateTimeout(options.sessionTimeout); this.connectTimeoutHandler = null; + this.sessionTimeoutHandler = setTimeout(function() { + if ([STATES.CONNECTED, STATES.SYNC_CONNECTED].indexOf(this.state) == -1) { + this.setState(STATES.SESSION_EXPIRED); + } + }.bind(this), this.sessionTimeout); + + this.xid = 0; this.sessionId = new Buffer(8); @@ -305,6 +312,7 @@ ConnectionManager.prototype.onSocketConnected = function () { if (this.connectTimeoutHandler) { clearTimeout(this.connectTimeoutHandler); + clearTimeout(this.sessionTimeoutHandler); } connectRequest = new jute.Request(null, new jute.protocol.ConnectRequest( From 940f4d5c31b602e5f7935dd10717e60ce05c0f0d Mon Sep 17 00:00:00 2001 From: hufeng Date: Fri, 17 Apr 2015 23:48:56 +0800 Subject: [PATCH 2/7] fixed event name --- lib/ConnectionManager.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/ConnectionManager.js b/lib/ConnectionManager.js index 8cf3ace..c1ad078 100644 --- a/lib/ConnectionManager.js +++ b/lib/ConnectionManager.js @@ -63,12 +63,11 @@ function ConnectionManager(connectionString, options, stateListener) { this.connectTimeoutHandler = null; this.sessionTimeoutHandler = setTimeout(function() { - if ([STATES.CONNECTED, STATES.SYNC_CONNECTED].indexOf(this.state) == -1) { + if ([STATES.CONNECTED, STATES.CONNECTED_READ_ONLY].indexOf(this.state) == -1) { this.setState(STATES.SESSION_EXPIRED); } }.bind(this), this.sessionTimeout); - this.xid = 0; this.sessionId = new Buffer(8); From 123971187c03f15d51623f1297032f425ac858d6 Mon Sep 17 00:00:00 2001 From: hufeng Date: Sat, 18 Apr 2015 10:50:01 +0800 Subject: [PATCH 3/7] fixed build failed --- lib/ConnectionManager.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/ConnectionManager.js b/lib/ConnectionManager.js index c1ad078..859fdc2 100644 --- a/lib/ConnectionManager.js +++ b/lib/ConnectionManager.js @@ -62,11 +62,16 @@ function ConnectionManager(connectionString, options, stateListener) { this.updateTimeout(options.sessionTimeout); this.connectTimeoutHandler = null; + /** + * add sessionTimeoutHandler + * + * @type {number} + */ this.sessionTimeoutHandler = setTimeout(function() { if ([STATES.CONNECTED, STATES.CONNECTED_READ_ONLY].indexOf(this.state) == -1) { this.setState(STATES.SESSION_EXPIRED); } - }.bind(this), this.sessionTimeout); + }.bind(this), options.sessionTimeout); this.xid = 0; @@ -311,6 +316,7 @@ ConnectionManager.prototype.onSocketConnected = function () { if (this.connectTimeoutHandler) { clearTimeout(this.connectTimeoutHandler); + //clear sessionTimeoutHandler clearTimeout(this.sessionTimeoutHandler); } From bf2fd513b997497081fbea394c78cbd082c0e429 Mon Sep 17 00:00:00 2001 From: hufeng Date: Sat, 18 Apr 2015 11:31:19 +0800 Subject: [PATCH 4/7] fixed code style --- lib/ConnectionManager.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/ConnectionManager.js b/lib/ConnectionManager.js index 859fdc2..2924711 100644 --- a/lib/ConnectionManager.js +++ b/lib/ConnectionManager.js @@ -67,11 +67,10 @@ function ConnectionManager(connectionString, options, stateListener) { * * @type {number} */ - this.sessionTimeoutHandler = setTimeout(function() { - if ([STATES.CONNECTED, STATES.CONNECTED_READ_ONLY].indexOf(this.state) == -1) { - this.setState(STATES.SESSION_EXPIRED); - } - }.bind(this), options.sessionTimeout); + this.sessionTimeoutHandler = setTimeout( + this.onSessionTimeout.bind(this), + options.sessionTimeout + ); this.xid = 0; @@ -301,6 +300,14 @@ ConnectionManager.prototype.onSocketError = function (error) { // we will retry connect in that listener function. }; +ConnectionManager.prototype.onSessionTimeout = function() { + var connectStates = [STATES.CONNECTED, STATES.CONNECTED_READ_ONLY]; + + if (connectStates.indexOf(this.state) == -1) { + this.setState(STATES.SESSION_EXPIRED); + } +}; + ConnectionManager.prototype.onSocketConnectTimeout = function () { // Destroy the current socket so the socket closed event // will be trigger. From 68285b6c6a7fcda5c57b5cee103fd1f4407808e5 Mon Sep 17 00:00:00 2001 From: hufeng Date: Sat, 18 Apr 2015 11:34:25 +0800 Subject: [PATCH 5/7] fixed code style --- lib/ConnectionManager.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ConnectionManager.js b/lib/ConnectionManager.js index 2924711..ba18b6b 100644 --- a/lib/ConnectionManager.js +++ b/lib/ConnectionManager.js @@ -300,10 +300,10 @@ ConnectionManager.prototype.onSocketError = function (error) { // we will retry connect in that listener function. }; -ConnectionManager.prototype.onSessionTimeout = function() { +ConnectionManager.prototype.onSessionTimeout = function () { var connectStates = [STATES.CONNECTED, STATES.CONNECTED_READ_ONLY]; - if (connectStates.indexOf(this.state) == -1) { + if (connectStates.indexOf(this.state) === -1) { this.setState(STATES.SESSION_EXPIRED); } }; From 3d1f8210f934fdc20af202d2019fab9024991cf9 Mon Sep 17 00:00:00 2001 From: hufeng Date: Thu, 7 May 2015 17:59:17 +0800 Subject: [PATCH 6/7] fixed sessionTimeout --- lib/ConnectionManager.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/ConnectionManager.js b/lib/ConnectionManager.js index ba18b6b..0d0ddef 100644 --- a/lib/ConnectionManager.js +++ b/lib/ConnectionManager.js @@ -67,10 +67,7 @@ function ConnectionManager(connectionString, options, stateListener) { * * @type {number} */ - this.sessionTimeoutHandler = setTimeout( - this.onSessionTimeout.bind(this), - options.sessionTimeout - ); + this.sessionTimeoutHandler = null; this.xid = 0; @@ -229,6 +226,11 @@ ConnectionManager.prototype.connect = function () { self.findNextServer(function (server) { self.socket = net.connect(server); + self.sessionTimeoutHandler = setTimeout( + self.onSessionTimeout.bind(self), + self.options.sessionTimeout + ); + self.connectTimeoutHandler = setTimeout( self.onSocketConnectTimeout.bind(self), self.connectTimeout @@ -306,6 +308,8 @@ ConnectionManager.prototype.onSessionTimeout = function () { if (connectStates.indexOf(this.state) === -1) { this.setState(STATES.SESSION_EXPIRED); } + + if(this.socket) this.socket.destroy(); }; ConnectionManager.prototype.onSocketConnectTimeout = function () { From c540b84a9d04d0ab094c1d1df40bed71164eaba1 Mon Sep 17 00:00:00 2001 From: hufeng Date: Thu, 7 May 2015 23:07:29 +0800 Subject: [PATCH 7/7] fixed code style --- lib/ConnectionManager.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ConnectionManager.js b/lib/ConnectionManager.js index 0d0ddef..77475cd 100644 --- a/lib/ConnectionManager.js +++ b/lib/ConnectionManager.js @@ -309,7 +309,9 @@ ConnectionManager.prototype.onSessionTimeout = function () { this.setState(STATES.SESSION_EXPIRED); } - if(this.socket) this.socket.destroy(); + if (this.socket) { + this.socket.destroy(); + } }; ConnectionManager.prototype.onSocketConnectTimeout = function () {