You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-10 11:43:01 +03:00
ref #1671 - add support for defaults
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import calculateSlot from 'cluster-key-slot';
|
import calculateSlot from 'cluster-key-slot';
|
||||||
import RedisClient, { RedisClientType } from './client';
|
import RedisClient, { RedisClientType } from './client';
|
||||||
import { RedisSocketOptions } from './socket';
|
|
||||||
import { RedisClusterMasterNode, RedisClusterReplicaNode } from './commands/CLUSTER_NODES';
|
import { RedisClusterMasterNode, RedisClusterReplicaNode } from './commands/CLUSTER_NODES';
|
||||||
import { RedisClusterClientOptions, RedisClusterOptions } from './cluster';
|
import { RedisClusterClientOptions, RedisClusterOptions } from './cluster';
|
||||||
import { RedisModules } from './commands';
|
import { RedisModules } from './commands';
|
||||||
@@ -32,7 +31,7 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisLu
|
|||||||
|
|
||||||
async connect(): Promise<void> {
|
async connect(): Promise<void> {
|
||||||
for (const rootNode of this.#options.rootNodes) {
|
for (const rootNode of this.#options.rootNodes) {
|
||||||
if (await this.#discoverNodes(rootNode)) return;
|
if (await this.#discoverNodes(this.#clientOptionsDefaults(rootNode))) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error('None of the root nodes is available');
|
throw new Error('None of the root nodes is available');
|
||||||
@@ -99,6 +98,18 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisLu
|
|||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#clientOptionsDefaults(options: RedisClusterClientOptions): RedisClusterClientOptions {
|
||||||
|
if (!this.#options.defaults) return options;
|
||||||
|
|
||||||
|
const merged = Object.assign({}, this.#options.defaults, options);
|
||||||
|
|
||||||
|
if (options.socket && this.#options.defaults.socket) {
|
||||||
|
Object.assign({}, this.#options.defaults.socket, options.socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
return merged;
|
||||||
|
}
|
||||||
|
|
||||||
#initiateClientForNode(nodeData: RedisClusterMasterNode | RedisClusterReplicaNode, readonly: boolean, clientsInUse: Set<string>, promises: Array<Promise<void>>): ClusterNode<M, S> {
|
#initiateClientForNode(nodeData: RedisClusterMasterNode | RedisClusterReplicaNode, readonly: boolean, clientsInUse: Set<string>, promises: Array<Promise<void>>): ClusterNode<M, S> {
|
||||||
const url = `${nodeData.host}:${nodeData.port}`;
|
const url = `${nodeData.host}:${nodeData.port}`;
|
||||||
clientsInUse.add(url);
|
clientsInUse.add(url);
|
||||||
@@ -107,13 +118,15 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisLu
|
|||||||
if (!node) {
|
if (!node) {
|
||||||
node = {
|
node = {
|
||||||
id: nodeData.id,
|
id: nodeData.id,
|
||||||
client: RedisClient.create({
|
client: RedisClient.create(
|
||||||
socket: {
|
this.#clientOptionsDefaults({
|
||||||
host: nodeData.host,
|
socket: {
|
||||||
port: nodeData.port
|
host: nodeData.host,
|
||||||
},
|
port: nodeData.port
|
||||||
readonly
|
},
|
||||||
})
|
readonly
|
||||||
|
})
|
||||||
|
)
|
||||||
};
|
};
|
||||||
promises.push(node.client.connect());
|
promises.push(node.client.connect());
|
||||||
this.#nodeByUrl.set(url, node);
|
this.#nodeByUrl.set(url, node);
|
||||||
|
@@ -10,7 +10,7 @@ export type RedisClusterClientOptions = Omit<RedisClientOptions<{}, {}>, 'module
|
|||||||
|
|
||||||
export interface RedisClusterOptions<M = {}, S = {}> {
|
export interface RedisClusterOptions<M = {}, S = {}> {
|
||||||
rootNodes: Array<RedisClusterClientOptions>;
|
rootNodes: Array<RedisClusterClientOptions>;
|
||||||
defaults?: RedisClusterClientOptions;
|
defaults?: Partial<RedisClusterClientOptions>;
|
||||||
modules?: M;
|
modules?: M;
|
||||||
scripts?: S;
|
scripts?: S;
|
||||||
useReplicas?: boolean;
|
useReplicas?: boolean;
|
||||||
|
Reference in New Issue
Block a user