From 60776e6cd2a9f6ee26e87166f3ea9e5e61c0ff21 Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Fri, 11 Jun 2021 12:32:20 +0800 Subject: [PATCH 1/2] 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 25e34ed4240..5faa24c9f88 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 }); From e5a6836ab4c2a9ff6471ad8a07564d3c3ce1660c Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Thu, 2 Dec 2021 18:06:01 +0800 Subject: [PATCH 2/2] clear retry timer first then emit close event --- lib/nodejs/lib/thrift/connection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nodejs/lib/thrift/connection.js b/lib/nodejs/lib/thrift/connection.js index 5faa24c9f88..b4e5a05277d 100644 --- a/lib/nodejs/lib/thrift/connection.js +++ b/lib/nodejs/lib/thrift/connection.js @@ -202,11 +202,11 @@ Connection.prototype.connection_gone = function () { // 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; } + self.emit("close"); return; }