MCPcopy
hub / github.com/redis/ioredis / findOrCreate

Method findOrCreate

lib/cluster/ConnectionPool.ts:100–147  ·  view source on GitHub ↗

* Find or create a connection to the node

(node: RedisOptions, readOnly = false)

Source from the content-addressed store, hash-verified

98 * Find or create a connection to the node
99 */
100 findOrCreate(node: RedisOptions, readOnly = false): Redis {
101 const key = getNodeKey(node);
102 readOnly = Boolean(readOnly);
103
104 if (this.specifiedOptions[key]) {
105 Object.assign(node, this.specifiedOptions[key]);
106 } else {
107 this.specifiedOptions[key] = node;
108 }
109
110 let redis: Redis;
111 if (this.nodes.all[key]) {
112 redis = this.nodes.all[key];
113 if (redis.options.readOnly !== readOnly) {
114 redis.options.readOnly = readOnly;
115 debug("Change role of %s to %s", key, readOnly ? "slave" : "master");
116 redis[readOnly ? "readonly" : "readwrite"]().catch(noop);
117 if (readOnly) {
118 delete this.nodes.master[key];
119 this.nodes.slave[key] = redis;
120 } else {
121 delete this.nodes.slave[key];
122 this.nodes.master[key] = redis;
123 }
124 }
125 } else {
126 debug("Connecting to %s as %s", key, readOnly ? "slave" : "master");
127 redis = this.createRedisFromOptions(node, readOnly);
128 this.nodes.all[key] = redis;
129 this.nodes[readOnly ? "slave" : "master"][key] = redis;
130
131 redis.once("end", () => {
132 this.removeNode(key);
133 this.emit("-node", redis, key);
134 if (!Object.keys(this.nodes.all).length) {
135 this.emit("drain");
136 }
137 });
138
139 this.emit("+node", redis, key);
140
141 redis.on("error", (error) => {
142 this.emit("nodeError", error, key);
143 });
144 }
145
146 return redis;
147 }
148
149 /**
150 * Reset the pool with a set of nodes.

Callers 4

resetMethod · 0.95
sendCommandMethod · 0.80
ConnectionPool.tsFile · 0.80

Calls 6

removeNodeMethod · 0.95
getNodeKeyFunction · 0.90
onceMethod · 0.80
keysMethod · 0.80
onMethod · 0.65

Tested by

no test coverage detected