Skip to content

Commit

Permalink
fix: when refreshSlotsCache is callback concurrently, call the call…
Browse files Browse the repository at this point in the history
…back only when the refresh process is done
  • Loading branch information
leibale committed Apr 16, 2024
1 parent ec42c82 commit 26cc9e5
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class Cluster extends Commander {
private slotsTimer: NodeJS.Timer;
private reconnectTimeout: NodeJS.Timer;
private isRefreshing = false;
private _refreshSlotsCacheCallbacks = [];
private _autoPipelines: Map<string, typeof Pipeline> = new Map();
private _runningAutoPipelines: Set<string> = new Set();
private _readyDelayedCallbacks: Callback[] = [];
Expand Down Expand Up @@ -411,20 +412,23 @@ class Cluster extends Commander {
* @ignore
*/
refreshSlotsCache(callback?: Callback<void>): void {
if (callback) {
this._refreshSlotsCacheCallbacks.push(callback);
}

if (this.isRefreshing) {
if (callback) {
process.nextTick(callback);
}
return;
}

this.isRefreshing = true;

const _this = this;
const wrapper = (error?: Error) => {
this.isRefreshing = false;
if (callback) {
for (const callback of this._refreshSlotsCacheCallbacks) {
callback(error);
}
this._refreshSlotsCacheCallbacks = [];
};

const nodes = shuffle(this.connectionPool.getNodes());
Expand Down

0 comments on commit 26cc9e5

Please sign in to comment.