-
Notifications
You must be signed in to change notification settings - Fork 419
/
redis-connection.ts
285 lines (239 loc) · 7.66 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
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 {
isNotConnectionError,
isRedisCluster,
isRedisInstance,
isRedisVersionLowerThan,
} from '../utils';
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: DEPRECATION WARNING! Your redis options maxRetriesPerRequest must be null.',
'On the next versions having this settings will throw an exception',
].join(' ');
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;
protected _client: RedisClient;
private readonly opts: RedisOptions;
private initializing: Promise<RedisClient>;
private version: string;
private handleClientError: (e: Error) => void;
private handleClientClose: () => void;
private handleClientReady: () => void;
constructor(
opts?: ConnectionOptions,
private readonly shared: boolean = false,
private readonly blocking = true,
) {
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);
}
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) {
if (this.blocking && options && options.maxRetriesPerRequest) {
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);
}
return new Promise<void>((resolve, reject) => {
let lastError: Error;
const errorHandler = (err: Error) => {
lastError = err;
};
const handleReady = () => {
client.removeListener('end', endHandler);
client.removeListener('error', errorHandler);
resolve();
};
const endHandler = () => {
client.removeListener('ready', handleReady);
client.removeListener('error', errorHandler);
reject(lastError || new Error(CONNECTION_CLOSED_ERROR_MSG));
};
client.once('ready', handleReady);
client.on('end', endHandler);
client.once('error', errorHandler);
});
}
get client(): Promise<RedisClient> {
return this.initializing;
}
protected loadCommands(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
if (!(<any>this._client)[finalScripts[property].name]) {
(<any>this._client).defineCommand(finalScripts[property].name, {
numberOfKeys: finalScripts[property].keys,
lua: finalScripts[property].content,
});
}
}
}
private async init() {
if (!this._client) {
this._client = new IORedis(this.opts);
}
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.version = await this.getRedisVersion();
if (this.opts && this.opts.skipVersionCheck !== true && !this.closing) {
if (
isRedisVersionLowerThan(this.version, RedisConnection.minimumVersion)
) {
throw new Error(
`Redis version needs to be greater 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}`,
);
}
}
return this._client;
}
async disconnect(): Promise<void> {
const client = await this.client;
if (client.status !== 'end') {
let _resolve, _reject;
const disconnecting = new Promise<void>((resolve, reject) => {
client.once('end', resolve);
client.once('error', reject);
_resolve = resolve;
_reject = reject;
});
client.disconnect();
try {
await disconnecting;
} finally {
client.removeListener('end', _resolve);
client.removeListener('error', _reject);
}
}
}
async reconnect(): Promise<void> {
const client = await this.client;
return client.connect();
}
async close(): Promise<void> {
if (!this.closing) {
this.closing = true;
try {
await this.initializing;
if (!this.shared) {
await this._client.quit();
}
} 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);
}
}
}
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;
}
}