-
Notifications
You must be signed in to change notification settings - Fork 406
/
redis-connection.ts
373 lines (312 loc) · 9.94 KB
/
redis-connection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import { EventEmitter } from 'events';
import { default as IORedis } from 'ioredis';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { CONNECTION_CLOSED_ERROR_MSG } from 'ioredis/built/utils';
import { ConnectionOptions, RedisOptions, RedisClient } from '../interfaces';
import {
decreaseMaxListeners,
increaseMaxListeners,
isNotConnectionError,
isRedisCluster,
isRedisInstance,
isRedisVersionLowerThan,
} from '../utils';
import { version as packageVersion } from '../version';
import * as scripts from '../scripts';
const overrideMessage = [
'BullMQ: WARNING! Your redis options maxRetriesPerRequest must be null',
'and will be overridden by BullMQ.',
].join(' ');
const deprecationMessage =
'BullMQ: Your redis options maxRetriesPerRequest must be null.';
interface RedisCapabilities {
canDoubleTimeout: boolean;
canBlockFor1Ms: boolean;
}
export interface RawCommand {
content: string;
name: string;
keys: number;
}
export class RedisConnection extends EventEmitter {
static minimumVersion = '5.0.0';
static recommendedMinimumVersion = '6.2.0';
closing: boolean;
capabilities: RedisCapabilities = {
canDoubleTimeout: false,
canBlockFor1Ms: true,
};
status: 'initializing' | 'ready' | 'closing' | 'closed' = 'initializing';
protected _client: RedisClient;
private readonly opts: RedisOptions;
private readonly initializing: Promise<RedisClient>;
private version: string;
protected packageVersion = packageVersion;
private skipVersionCheck: boolean;
private handleClientError: (e: Error) => void;
private handleClientClose: () => void;
private handleClientReady: () => void;
constructor(
opts?: ConnectionOptions,
private readonly shared: boolean = false,
private readonly blocking = true,
skipVersionCheck = false,
) {
super();
if (!isRedisInstance(opts)) {
this.checkBlockingOptions(overrideMessage, opts);
this.opts = {
port: 6379,
host: '127.0.0.1',
retryStrategy: function (times: number) {
return Math.max(Math.min(Math.exp(times), 20000), 1000);
},
...opts,
};
if (this.blocking) {
this.opts.maxRetriesPerRequest = null;
}
} else {
this._client = opts;
// Test if the redis instance is using keyPrefix
// and if so, throw an error.
if (this._client.options.keyPrefix) {
throw new Error(
'BullMQ: ioredis does not support ioredis prefixes, use the prefix option instead.',
);
}
if (isRedisCluster(this._client)) {
this.opts = this._client.options.redisOptions;
} else {
this.opts = this._client.options;
}
this.checkBlockingOptions(deprecationMessage, this.opts, true);
}
this.skipVersionCheck =
skipVersionCheck || !!(this.opts && this.opts.skipVersionCheck);
this.handleClientError = (err: Error): void => {
this.emit('error', err);
};
this.handleClientClose = (): void => {
this.emit('close');
};
this.handleClientReady = (): void => {
this.emit('ready');
};
this.initializing = this.init();
this.initializing.catch(err => this.emit('error', err));
}
private checkBlockingOptions(
msg: string,
options?: RedisOptions,
throwError = false,
) {
if (this.blocking && options && options.maxRetriesPerRequest) {
if (throwError) {
throw new Error(msg);
} else {
console.error(msg);
}
}
}
/**
* Waits for a redis client to be ready.
* @param redis - client
*/
static async waitUntilReady(client: RedisClient): Promise<void> {
if (client.status === 'ready') {
return;
}
if (client.status === 'wait') {
return client.connect();
}
if (client.status === 'end') {
throw new Error(CONNECTION_CLOSED_ERROR_MSG);
}
let handleReady: () => void;
let handleEnd: () => void;
let handleError: (e: Error) => void;
try {
await new Promise<void>((resolve, reject) => {
let lastError: Error;
handleError = (err: Error) => {
lastError = err;
};
handleReady = () => {
resolve();
};
handleEnd = () => {
if (client.status !== 'end') {
reject(lastError || new Error(CONNECTION_CLOSED_ERROR_MSG));
} else {
if (lastError) {
reject(lastError);
} else {
// when custon 'end' status is set we already closed
resolve();
}
}
};
increaseMaxListeners(client, 3);
client.once('ready', handleReady);
client.on('end', handleEnd);
client.once('error', handleError);
});
} finally {
client.removeListener('end', handleEnd);
client.removeListener('error', handleError);
client.removeListener('ready', handleReady);
decreaseMaxListeners(client, 3);
}
}
get client(): Promise<RedisClient> {
return this.initializing;
}
protected loadCommands(
packageVersion: string,
providedScripts?: Record<string, RawCommand>,
): void {
const finalScripts =
providedScripts || (scripts as Record<string, RawCommand>);
for (const property in finalScripts as Record<string, RawCommand>) {
// Only define the command if not already defined
const commandName = `${finalScripts[property].name}:${packageVersion}`;
if (!(<any>this._client)[commandName]) {
(<any>this._client).defineCommand(commandName, {
numberOfKeys: finalScripts[property].keys,
lua: finalScripts[property].content,
});
}
}
}
private async init() {
if (!this._client) {
const { url, ...rest } = this.opts;
this._client = url ? new IORedis(url, rest) : new IORedis(rest);
}
increaseMaxListeners(this._client, 3);
this._client.on('error', this.handleClientError);
// ioredis treats connection errors as a different event ('close')
this._client.on('close', this.handleClientClose);
this._client.on('ready', this.handleClientReady);
await RedisConnection.waitUntilReady(this._client);
this.loadCommands(this.packageVersion);
if (this._client['status'] !== 'end') {
this.version = await this.getRedisVersion();
if (this.skipVersionCheck !== true && !this.closing) {
if (
isRedisVersionLowerThan(this.version, RedisConnection.minimumVersion)
) {
throw new Error(
`Redis version needs to be greater or equal than ${RedisConnection.minimumVersion} ` +
`Current: ${this.version}`,
);
}
if (
isRedisVersionLowerThan(
this.version,
RedisConnection.recommendedMinimumVersion,
)
) {
console.warn(
`It is highly recommended to use a minimum Redis version of ${RedisConnection.recommendedMinimumVersion}
Current: ${this.version}`,
);
}
}
this.capabilities = {
canDoubleTimeout: !isRedisVersionLowerThan(this.version, '6.0.0'),
canBlockFor1Ms: !isRedisVersionLowerThan(this.version, '7.0.8'),
};
this.status = 'ready';
}
return this._client;
}
async disconnect(wait = true): Promise<void> {
const client = await this.client;
if (client.status !== 'end') {
let _resolve, _reject;
if (!wait) {
return client.disconnect();
}
const disconnecting = new Promise<void>((resolve, reject) => {
increaseMaxListeners(client, 2);
client.once('end', resolve);
client.once('error', reject);
_resolve = resolve;
_reject = reject;
});
client.disconnect();
try {
await disconnecting;
} finally {
decreaseMaxListeners(client, 2);
client.removeListener('end', _resolve);
client.removeListener('error', _reject);
}
}
}
async reconnect(): Promise<void> {
const client = await this.client;
return client.connect();
}
async close(force = false): Promise<void> {
if (!this.closing) {
const status = this.status;
this.status = 'closing';
this.closing = true;
try {
if (status === 'ready') {
// Not sure if we need to wait for this
await this.initializing;
}
if (!this.shared) {
if (status == 'initializing' || force) {
// If we have not still connected to Redis, we need to disconnect.
this._client.disconnect();
} else {
await this._client.quit();
}
// As IORedis does not update this status properly, we do it ourselves.
this._client['status'] = 'end';
}
} catch (error) {
if (isNotConnectionError(error as Error)) {
throw error;
}
} finally {
this._client.off('error', this.handleClientError);
this._client.off('close', this.handleClientClose);
this._client.off('ready', this.handleClientReady);
decreaseMaxListeners(this._client, 3);
this.removeAllListeners();
this.status = 'closed';
}
}
}
private async getRedisVersion() {
const doc = await this._client.info();
const redisPrefix = 'redis_version:';
const maxMemoryPolicyPrefix = 'maxmemory_policy:';
const lines = doc.split('\r\n');
let redisVersion;
for (let i = 0; i < lines.length; i++) {
if (lines[i].indexOf(maxMemoryPolicyPrefix) === 0) {
const maxMemoryPolicy = lines[i].substr(maxMemoryPolicyPrefix.length);
if (maxMemoryPolicy !== 'noeviction') {
console.warn(
`IMPORTANT! Eviction policy is ${maxMemoryPolicy}. It should be "noeviction"`,
);
}
}
if (lines[i].indexOf(redisPrefix) === 0) {
redisVersion = lines[i].substr(redisPrefix.length);
}
}
return redisVersion;
}
get redisVersion(): string {
return this.version;
}
}