Skip to content

Commit 57327ce

Browse files
authored
fix(client): return Promise consistently in cluster functions (redis#3159)
It is confusing for the same function to sometimes return an object and sometimes return a Promise.
1 parent 0d8f3a2 commit 57327ce

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

packages/client/lib/cluster/cluster-slots.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,15 @@ export default class RedisClusterSlots<
370370
.finally(() => node.connectPromise = undefined);
371371
}
372372

373-
nodeClient(node: ShardNode<M, F, S, RESP, TYPE_MAPPING>) {
374-
return (
375-
node.connectPromise ?? // if the node is connecting
376-
node.client ?? // if the node is connected
377-
this.#createNodeClient(node) // if the not is disconnected
378-
);
373+
nodeClient(node: ShardNode<M, F, S, RESP, TYPE_MAPPING>): Promise<RedisClientType<M, F, S, RESP, TYPE_MAPPING>> {
374+
// if the node is connecting
375+
if (node.connectPromise)
376+
return node.connectPromise;
377+
// if the node is connected
378+
if (node.client)
379+
return Promise.resolve(node.client);
380+
// if the not is disconnected
381+
return this.#createNodeClient(node)
379382
}
380383

381384
#runningRediscoverPromise?: Promise<void>;
@@ -553,10 +556,10 @@ export default class RedisClusterSlots<
553556
return this.nodeClient(master);
554557
}
555558

556-
getPubSubClient() {
559+
getPubSubClient(): Promise<RedisClientType<M, F, S, RESP, TYPE_MAPPING>> {
557560
if (!this.pubSubNode) return this.#initiatePubSubClient();
558561

559-
return this.pubSubNode.connectPromise ?? this.pubSubNode.client;
562+
return this.pubSubNode.connectPromise ?? Promise.resolve(this.pubSubNode.client);
560563
}
561564

562565
async #initiatePubSubClient(toResubscribe?: PubSubToResubscribe) {
@@ -602,10 +605,10 @@ export default class RedisClusterSlots<
602605
}
603606
}
604607

605-
getShardedPubSubClient(channel: string) {
608+
getShardedPubSubClient(channel: string): Promise<RedisClientType<M, F, S, RESP, TYPE_MAPPING>> {
606609
const { master } = this.slots[calculateSlot(channel)];
607610
if (!master.pubSub) return this.#initiateShardedPubSubClient(master);
608-
return master.pubSub.connectPromise ?? master.pubSub.client;
611+
return master.pubSub.connectPromise ?? Promise.resolve(master.pubSub.client);
609612
}
610613

611614
async #initiateShardedPubSubClient(master: MasterNode<M, F, S, RESP, TYPE_MAPPING>) {

packages/client/lib/sentinel/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,10 +740,10 @@ class RedisSentinelInternal<
740740
* @returns A client info object or a promise that resolves to a client info object
741741
* when a client becomes available
742742
*/
743-
getClientLease(): ClientInfo | Promise<ClientInfo> {
743+
getClientLease(): Promise<ClientInfo> {
744744
const id = this.#masterClientQueue.shift();
745745
if (id !== undefined) {
746-
return { id };
746+
return Promise.resolve({ id });
747747
}
748748

749749
return this.#masterClientQueue.wait().then(id => ({ id }));

0 commit comments

Comments
 (0)