-
Notifications
You must be signed in to change notification settings - Fork 358
/
redis-connection.ts
226 lines (190 loc) · 6.09 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
import { EventEmitter } from 'events';
import * 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 * as semver from 'semver';
import { scriptLoader } from '../commands';
import { ConnectionOptions, RedisOptions, RedisClient } from '../interfaces';
import { isRedisInstance, isNotConnectionError } from '../utils';
import * as path from 'path';
const overrideMessage = [
'BullMQ: WARNING! Your redis options maxRetriesPerRequest must be null and enableReadyCheck false',
'and will be overridden by BullMQ.',
].join(' ');
const connectionErrorMessage = `Using a redis instance with enableReadyCheck or maxRetriesPerRequest is not permitted.
See https://https://github.com/OptimalBits/bull/issues/1873`;
export class RedisConnection extends EventEmitter {
static minimumVersion = '5.0.0';
protected _client: RedisClient;
private initializing: Promise<RedisClient>;
private closing: boolean;
private version: string;
private handleClientError: (e: Error) => void;
constructor(
private readonly opts?: ConnectionOptions,
private readonly shared: boolean = false,
) {
super();
if (!isRedisInstance(opts)) {
this.checkOptions(overrideMessage, <RedisOptions>opts);
this.opts = {
port: 6379,
host: '127.0.0.1',
retryStrategy: function (times: number) {
return Math.min(Math.exp(times), 20000);
},
...opts,
maxRetriesPerRequest: null,
enableReadyCheck: false,
};
} else {
this._client = <RedisClient>opts;
let options = this._client.options;
if((<IORedis.ClusterOptions>options)?.redisOptions){
options = (<IORedis.ClusterOptions>options).redisOptions;
}
if (
(<IORedis.RedisOptions>options).maxRetriesPerRequest ||
options.enableReadyCheck
) {
throw new Error(connectionErrorMessage);
}
}
this.handleClientError = (err: Error): void => {
this.emit('error', err);
};
this.initializing = this.init();
this.initializing.catch(err => this.emit('error', err));
}
private checkOptions(msg: string, options?: IORedis.RedisOptions) {
if (
options &&
(options.maxRetriesPerRequest || options.enableReadyCheck)
) {
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(): Promise<void> {
return scriptLoader.load(this._client, path.join(__dirname, '../commands'));
}
private async init() {
const opts = this.opts as RedisOptions;
if (!this._client) {
this._client = new IORedis(opts);
}
this._client.on('error', this.handleClientError);
await RedisConnection.waitUntilReady(this._client);
await this.loadCommands();
if (opts && opts.skipVersionCheck !== true && !this.closing) {
this.version = await this.getRedisVersion();
if (semver.lt(this.version, RedisConnection.minimumVersion)) {
throw new Error(
`Redis version needs to be greater than ${RedisConnection.minimumVersion} 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);
}
}
}
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') {
throw new Error(
`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;
}
}