Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix reconnect issue for nodejs #2407

Merged
merged 4 commits into from
Oct 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lib/nodejs/lib/thrift/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
};

Expand Down Expand Up @@ -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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be overly pedantic, but would it be better to first clear the timer and then emit close? In other words, move the line self.emit("close"); down below the if (this.retry_timer) { block?

Copy link
Contributor Author

@wujjpp wujjpp Dec 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, close event handler maybe cause panic.
I have updated this change

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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
});
Expand Down