Skip to content

Commit 804ee07

Browse files
authored
fix: when refreshSlotsCache is callback concurrently, call the callback only when the refresh process is done (#1881)
1 parent 673ac77 commit 804ee07

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Diff for: lib/cluster/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class Cluster extends Commander {
9999
private slotsTimer: NodeJS.Timer;
100100
private reconnectTimeout: NodeJS.Timer;
101101
private isRefreshing = false;
102+
private _refreshSlotsCacheCallbacks = [];
102103
private _autoPipelines: Map<string, typeof Pipeline> = new Map();
103104
private _runningAutoPipelines: Set<string> = new Set();
104105
private _readyDelayedCallbacks: Callback[] = [];
@@ -411,20 +412,23 @@ class Cluster extends Commander {
411412
* @ignore
412413
*/
413414
refreshSlotsCache(callback?: Callback<void>): void {
415+
if (callback) {
416+
this._refreshSlotsCacheCallbacks.push(callback);
417+
}
418+
414419
if (this.isRefreshing) {
415-
if (callback) {
416-
process.nextTick(callback);
417-
}
418420
return;
419421
}
422+
420423
this.isRefreshing = true;
421424

422425
const _this = this;
423426
const wrapper = (error?: Error) => {
424427
this.isRefreshing = false;
425-
if (callback) {
428+
for (const callback of this._refreshSlotsCacheCallbacks) {
426429
callback(error);
427430
}
431+
this._refreshSlotsCacheCallbacks = [];
428432
};
429433

430434
const nodes = shuffle(this.connectionPool.getNodes());

0 commit comments

Comments
 (0)