From 8940715a1b8c2808cd2654687552dbbda3509efa Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Fri, 11 Jun 2021 12:32:20 +0800 Subject: fix reconnect issue for nodejs --- lib/nodejs/lib/thrift/connection.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/nodejs/lib/thrift/connection.js b/lib/nodejs/lib/thrift/connection.js index 25e34ed42..5faa24c9f 100644 --- a/lib/nodejs/lib/thrift/connection.js +++ b/lib/nodejs/lib/thrift/connection.js @@ -44,6 +44,7 @@ var Connection = exports.Connection = function(stream, options) { this.protocol = this.options.protocol || TBinaryProtocol; this.offline_queue = []; this.connected = false; + this.forceClose = false; this.initialize_retry_vars(); this._debug = this.options.debug || false; @@ -159,6 +160,7 @@ var Connection = exports.Connection = function(stream, options) { util.inherits(Connection, EventEmitter); Connection.prototype.end = function() { + this.forceClose = true; this.connection.end(); }; @@ -198,6 +200,16 @@ Connection.prototype.connection_gone = function () { var self = this; this.connected = false; + // If closed by manual, emit close event and cancel reconnect process + if(this.forceClose) { + self.emit("close"); + if (this.retry_timer) { + clearTimeout(this.retry_timer); + this.retry_timer = null; + } + return; + } + // If a retry is already in progress, just let that happen if (this.retry_timer) { return; @@ -230,11 +242,6 @@ Connection.prototype.connection_gone = function () { }); this.retry_timer = setTimeout(function () { - if (self.connection.destroyed) { - self.retry_timer = null; - return; - } - log.debug("Retrying connection..."); self.retry_totaltime += self.retry_delay; @@ -257,7 +264,7 @@ Connection.prototype.connection_gone = function () { exports.createConnection = function(host, port, options) { var stream = net.createConnection( { - port: port, + port: port, host: host, timeout: options.connect_timeout || options.timeout || 0 }); -- cgit v1.2.1