Skip to content

Commit

Permalink
Fixes #1063: PoolCluster#end now accepts a callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
Hasan Kumar committed Nov 20, 2019
1 parent 7dab7df commit df53220
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions lib/pool_cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,37 @@ class PoolCluster extends EventEmitter {
namespace.getConnection(cb);
}

end() {
end(callback) {
const cb =
callback !== undefined
? callback
: err => {
if (err) {
throw err;
}
};
if (this._closed) {
process.nextTick(cb);
return;
}
this._closed = true;

let calledBack = false;
let waitingClose = 0;
const onEnd = err => {
if (!calledBack && (err || --waitingClose <= 0)) {
calledBack = true;
return cb(err);
}
};

for (const id in this._nodes) {
waitingClose++;
this._nodes[id].pool.end();
}
if (waitingClose === 0) {
process.nextTick(onEnd);
}
}

_findNodeIds(pattern) {
Expand All @@ -142,8 +165,8 @@ class PoolCluster extends EventEmitter {
} else {
// wild matching
const keyword = pattern.substring(pattern.length - 1, 0);
foundNodeIds = this._serviceableNodeIds.filter(
id => id.startsWith(keyword)
foundNodeIds = this._serviceableNodeIds.filter(id =>
id.startsWith(keyword)
);
}
this._findCaches[pattern] = foundNodeIds;
Expand Down Expand Up @@ -183,12 +206,11 @@ class PoolCluster extends EventEmitter {
// eslint-disable-next-line no-console
console.warn(`[Error] PoolCluster : ${err}`);
return cb(null, 'retry');
}
}
return cb(err);

}
}
this._decreaseErrorCount(node);

connection._clusterId = node.id;
return cb(null, connection);
});
Expand Down

0 comments on commit df53220

Please sign in to comment.