From b0f035ed30fa29fdb2c825e1b235a928ecd39406 Mon Sep 17 00:00:00 2001
From: artofspeed <joesmithz2008@gmail.com>
Date: Sun, 7 Oct 2018 11:41:02 +0800
Subject: [PATCH] fixed ConnectionManager infinite connect retries

#37 PR in original repo
---
 lib/ConnectionManager.js | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/lib/ConnectionManager.js b/lib/ConnectionManager.js
index d3c0226..ac84125 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;
 
+    /**
+     * add sessionTimeoutHandler
+     *
+     * @type {number}
+     */
+    this.sessionTimeoutHandler = null;
+
     this.xid = 0;
 
     this.sessionId = new Buffer(8);
@@ -218,6 +225,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),
@@ -290,6 +302,16 @@ 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);
+    }
+    if (this.socket) {
+        this.socket.destroy();
+    }
+};
+
 ConnectionManager.prototype.onSocketConnectTimeout = function () {
     // Destroy the current socket so the socket closed event
     // will be trigger.
@@ -305,6 +327,8 @@ ConnectionManager.prototype.onSocketConnected = function () {
 
     if (this.connectTimeoutHandler) {
         clearTimeout(this.connectTimeoutHandler);
+        //clear sessionTimeoutHandler
+        clearTimeout(this.sessionTimeoutHandler);
     }
 
     connectRequest = new jute.Request(null, new jute.protocol.ConnectRequest(