From 804ee071cab4326d1d69eec0e9d156aac4aa89f4 Mon Sep 17 00:00:00 2001 From: Leibale Eidelman Date: Tue, 16 Apr 2024 15:11:04 -0400 Subject: [PATCH] fix: when `refreshSlotsCache` is callback concurrently, call the callback only when the refresh process is done (#1881) --- lib/cluster/index.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/cluster/index.ts b/lib/cluster/index.ts index 8419b1a3..c6500f5e 100644 --- a/lib/cluster/index.ts +++ b/lib/cluster/index.ts @@ -99,6 +99,7 @@ class Cluster extends Commander { private slotsTimer: NodeJS.Timer; private reconnectTimeout: NodeJS.Timer; private isRefreshing = false; + private _refreshSlotsCacheCallbacks = []; private _autoPipelines: Map = new Map(); private _runningAutoPipelines: Set = new Set(); private _readyDelayedCallbacks: Callback[] = []; @@ -411,20 +412,23 @@ class Cluster extends Commander { * @ignore */ refreshSlotsCache(callback?: Callback): 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());