-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathAmqpConnectionManager.ts
455 lines (393 loc) · 17.9 KB
/
AmqpConnectionManager.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import * as amqp from 'amqplib';
import { EventEmitter, once } from 'events';
import { TcpSocketConnectOpts } from 'net';
import pb from 'promise-breaker';
import { ConnectionOptions } from 'tls';
import { URL } from 'url';
import ChannelWrapper, { CreateChannelOpts } from './ChannelWrapper.js';
import { wait } from './helpers.js';
// Default heartbeat time.
const HEARTBEAT_IN_SECONDS = 5;
export type ConnectionUrl =
| string
| amqp.Options.Connect
| { url: string; connectionOptions?: AmqpConnectionOptions };
export interface ConnectListener {
(arg: { connection: amqp.Connection; url: string | amqp.Options.Connect }): void;
}
export interface ConnectFailedListener {
(arg: { err: Error; url: string | amqp.Options.Connect | undefined }): void;
}
export type AmqpConnectionOptions = (ConnectionOptions | TcpSocketConnectOpts) & {
noDelay?: boolean;
timeout?: number;
keepAlive?: boolean;
keepAliveDelay?: number;
clientProperties?: any;
credentials?:
| {
mechanism: string;
username: string;
password: string;
response: () => Buffer;
}
| {
mechanism: string;
response: () => Buffer;
}
| undefined;
};
export interface AmqpConnectionManagerOptions {
/** Interval to send heartbeats to broker. Defaults to 5 seconds. */
heartbeatIntervalInSeconds?: number;
/**
* The time to wait before trying to reconnect. If not specified, defaults
* to `heartbeatIntervalInSeconds`.
*/
reconnectTimeInSeconds?: number | undefined;
/**
* `findServers` is a function that which returns one or more servers to
* connect to. This should return either a single URL or an array of URLs.
* This is handy when you're using a service discovery mechanism such as
* Consul or etcd. Instead of taking a callback, this can also return a
* Promise. Note that if this is supplied, then `urls` is ignored.
*/
findServers?:
| ((callback: (urls: ConnectionUrl | ConnectionUrl[]) => void) => void)
| (() => Promise<ConnectionUrl | ConnectionUrl[]>)
| undefined;
/** Connection options, passed as options to the amqplib.connect() method. */
connectionOptions?: AmqpConnectionOptions;
}
/* istanbul ignore next */
function neverThrows() {
return (err: Error) =>
setImmediate(() => {
throw new Error(
`AmqpConnectionManager - should never get here: ${err.message}\n` + err.stack
);
});
}
export interface IAmqpConnectionManager {
connectionOptions?: AmqpConnectionOptions;
heartbeatIntervalInSeconds: number;
reconnectTimeInSeconds: number;
addListener(event: string, listener: (...args: any[]) => void): this;
addListener(event: 'connect', listener: ConnectListener): this;
addListener(event: 'connectFailed', listener: ConnectFailedListener): this;
addListener(event: 'blocked', listener: (arg: { reason: string }) => void): this;
addListener(event: 'unblocked', listener: () => void): this;
addListener(event: 'disconnect', listener: (arg: { err: Error }) => void): this;
// eslint-disable-next-line @typescript-eslint/ban-types
listeners(eventName: string | symbol): Function[];
on(event: string, listener: (...args: any[]) => void): this;
on(event: 'connect', listener: ConnectListener): this;
on(event: 'connectFailed', listener: ConnectFailedListener): this;
on(event: 'blocked', listener: (arg: { reason: string }) => void): this;
on(event: 'unblocked', listener: () => void): this;
on(event: 'disconnect', listener: (arg: { err: Error }) => void): this;
once(event: string, listener: (...args: any[]) => void): this;
once(event: 'connect', listener: ConnectListener): this;
once(event: 'connectFailed', listener: ConnectFailedListener): this;
once(event: 'blocked', listener: (arg: { reason: string }) => void): this;
once(event: 'unblocked', listener: () => void): this;
once(event: 'disconnect', listener: (arg: { err: Error }) => void): this;
prependListener(event: string, listener: (...args: any[]) => void): this;
prependListener(event: 'connect', listener: ConnectListener): this;
prependListener(event: 'connectFailed', listener: ConnectFailedListener): this;
prependListener(event: 'blocked', listener: (arg: { reason: string }) => void): this;
prependListener(event: 'unblocked', listener: () => void): this;
prependListener(event: 'disconnect', listener: (arg: { err: Error }) => void): this;
prependOnceListener(event: string, listener: (...args: any[]) => void): this;
prependOnceListener(event: 'connect', listener: ConnectListener): this;
prependOnceListener(event: 'connectFailed', listener: ConnectFailedListener): this;
prependOnceListener(event: 'blocked', listener: (arg: { reason: string }) => void): this;
prependOnceListener(event: 'unblocked', listener: () => void): this;
prependOnceListener(event: 'disconnect', listener: (arg: { err: Error }) => void): this;
removeListener(event: string, listener: (...args: any[]) => void): this;
connect(options?: { timeout?: number }): Promise<void>;
reconnect(): void;
createChannel(options?: CreateChannelOpts): ChannelWrapper;
close(): Promise<void>;
isConnected(): boolean;
/** The current connection. */
readonly connection: amqp.Connection | undefined;
/** Returns the number of registered channels. */
readonly channelCount: number;
}
//
// Events:
// * `connect({connection, url})` - Emitted whenever we connect to a broker.
// * `connectFailed({err, url})` - Emitted whenever we fail to connect to a broker.
// * `disconnect({err})` - Emitted whenever we disconnect from a broker.
// * `blocked({reason})` - Emitted whenever connection is blocked by a broker.
// * `unblocked()` - Emitted whenever connection is unblocked by a broker.
//
export default class AmqpConnectionManager extends EventEmitter implements IAmqpConnectionManager {
private _channels: ChannelWrapper[];
private _currentUrl: number;
private _closed = false;
private _cancelRetriesHandler?: () => void;
private _connectPromise?: Promise<null>;
private _currentConnection?: amqp.Connection;
private _findServers:
| ((callback: (urls: ConnectionUrl | ConnectionUrl[]) => void) => void)
| (() => Promise<ConnectionUrl | ConnectionUrl[]>);
private _urls?: ConnectionUrl[];
public connectionOptions: AmqpConnectionOptions | undefined;
public heartbeatIntervalInSeconds: number;
public reconnectTimeInSeconds: number;
/**
* Create a new AmqplibConnectionManager.
*
* @param urls - An array of brokers to connect to.
* Takes url strings or objects {url: string, connectionOptions?: object}
* If present, a broker's [connectionOptions] will be used instead
* of [options.connectionOptions] when passed to the amqplib connect method.
* AmqplibConnectionManager will round-robin between them whenever it
* needs to create a new connection.
* @param [options={}] -
* @param [options.heartbeatIntervalInSeconds=5] - The interval,
* in seconds, to send heartbeats.
* @param [options.reconnectTimeInSeconds] - The time to wait
* before trying to reconnect. If not specified, defaults to
* `heartbeatIntervalInSeconds`.
* @param [options.connectionOptions] - Passed to the amqplib
* connect method.
* @param [options.findServers] - A `fn(callback)` or a `fn()`
* which returns a Promise. This should resolve to one or more servers
* to connect to, either a single URL or an array of URLs. This is handy
* when you're using a service discovery mechanism such as Consul or etcd.
* Note that if this is supplied, then `urls` is ignored.
*/
constructor(
urls: ConnectionUrl | ConnectionUrl[] | undefined | null,
options: AmqpConnectionManagerOptions = {}
) {
super();
if (!urls && !options.findServers) {
throw new Error('Must supply either `urls` or `findServers`');
}
this._channels = [];
this._currentUrl = 0;
this.connectionOptions = options.connectionOptions;
this.heartbeatIntervalInSeconds =
options.heartbeatIntervalInSeconds || options.heartbeatIntervalInSeconds === 0
? options.heartbeatIntervalInSeconds
: HEARTBEAT_IN_SECONDS;
this.reconnectTimeInSeconds =
options.reconnectTimeInSeconds || this.heartbeatIntervalInSeconds;
// There will be one listener per channel, and there could be a lot of channels, so disable warnings from node.
this.setMaxListeners(0);
this._findServers = options.findServers || (() => Promise.resolve(urls));
}
/**
* Start the connect retries and await the first connect result. Even if the initial connect fails or timeouts, the
* reconnect attempts will continue in the background.
* @param [options={}] -
* @param [options.timeout] - Time to wait for initial connect
*/
async connect({ timeout }: { timeout?: number } = {}): Promise<void> {
this._connect();
let reject: (reason?: any) => void;
const onConnectFailed = ({ err }: { err: Error }) => {
// Ignore disconnects caused bad credentials.
if (err.message.includes('ACCESS-REFUSED') || err.message.includes('403')) {
reject(err);
}
};
let waitTimeout;
if (timeout) {
waitTimeout = wait(timeout);
}
try {
await Promise.race([
once(this, 'connect'),
new Promise((_resolve, innerReject) => {
reject = innerReject;
this.on('connectFailed', onConnectFailed);
}),
...(waitTimeout
? [
waitTimeout.promise.then(() => {
throw new Error('amqp-connection-manager: connect timeout');
}),
]
: []),
]);
} finally {
waitTimeout?.cancel();
this.removeListener('connectFailed', onConnectFailed);
}
}
// `options` here are any options that can be passed to ChannelWrapper.
createChannel(options: CreateChannelOpts = {}): ChannelWrapper {
const channel = new ChannelWrapper(this, options);
this._channels.push(channel);
channel.once('close', () => {
this._channels = this._channels.filter((c) => c !== channel);
});
return channel;
}
close(): Promise<void> {
if (this._closed) {
return Promise.resolve();
}
this._closed = true;
if (this._cancelRetriesHandler) {
this._cancelRetriesHandler();
this._cancelRetriesHandler = undefined;
}
return Promise.resolve(this._connectPromise).then(() => {
return Promise.all(this._channels.map((channel) => channel.close()))
.catch(function () {
// Ignore errors closing channels.
})
.then(() => {
this._channels = [];
if (this._currentConnection) {
this._currentConnection.removeAllListeners('close');
return this._currentConnection.close();
} else {
return null;
}
})
.then(() => {
this._currentConnection = undefined;
});
});
}
isConnected(): boolean {
return !!this._currentConnection;
}
/** Force reconnect - noop unless connected */
reconnect(): void {
if (this._closed) {
throw new Error('cannot reconnect after close');
}
// If we have a connection, close it and immediately connect again.
// Wait for ordinary reconnect otherwise.
if (this._currentConnection) {
this._currentConnection.removeAllListeners();
this._currentConnection
.close()
.catch(() => {
// noop
})
.then(() => {
this._currentConnection = undefined;
this.emit('disconnect', { err: new Error('forced reconnect') });
return this._connect();
})
.catch(neverThrows);
}
}
/** The current connection. */
get connection(): amqp.Connection | undefined {
return this._currentConnection;
}
/** Returns the number of registered channels. */
get channelCount(): number {
return this._channels.length;
}
private _connect(): Promise<null> {
if (this._connectPromise) {
return this._connectPromise;
}
if (this._closed || this.isConnected()) {
return Promise.resolve(null);
}
let attemptedUrl: string | amqp.Options.Connect | undefined;
const result = (this._connectPromise = Promise.resolve()
.then(() => {
if (!this._urls || this._currentUrl >= this._urls.length) {
this._currentUrl = 0;
return pb.call(this._findServers, 0, null);
} else {
return this._urls;
}
})
.then((urls: ConnectionUrl | ConnectionUrl[] | undefined) => {
if (Array.isArray(urls)) {
this._urls = urls;
} else if (urls) {
this._urls = [urls];
}
if (!this._urls || this._urls.length === 0) {
throw new Error('amqp-connection-manager: No servers found');
}
// Round robin between brokers
const url = this._urls[this._currentUrl];
this._currentUrl++;
// Set connectionOptions to the setting in the class instance (which came via the constructor)
let connectionOptions: ConnectionOptions | undefined = this.connectionOptions;
let originalUrl: string | amqp.Options.Connect;
let connect: string | amqp.Options.Connect;
if (typeof url === 'object' && 'url' in url) {
originalUrl = connect = url.url;
// If URL is an object, pull out any specific URL connectionOptions for it or use the
// instance connectionOptions if none were provided for this specific URL.
connectionOptions = url.connectionOptions || this.connectionOptions;
} else if (typeof url === 'string') {
originalUrl = connect = url;
} else {
originalUrl = url;
connect = {
...url,
heartbeat: url.heartbeat ?? this.heartbeatIntervalInSeconds,
};
}
attemptedUrl = originalUrl;
// Add the `heartbeastIntervalInSeconds` to the connection options.
if (typeof connect === 'string') {
const u = new URL(connect);
if (!u.searchParams.get('heartbeat')) {
u.searchParams.set('heartbeat', `${this.heartbeatIntervalInSeconds}`);
}
connect = u.toString();
}
return amqp.connect(connect, connectionOptions).then((connection) => {
this._currentConnection = connection;
//emit 'blocked' when RabbitMQ server decides to block the connection (resources running low)
connection.on('blocked', (reason) => this.emit('blocked', { reason }));
connection.on('unblocked', () => this.emit('unblocked'));
connection.on('error', (/* err */) => {
// if this event was emitted, then the connection was already closed,
// so no need to call #close here
// also, 'close' is emitted after 'error',
// so no need for work already done in 'close' handler
});
// Reconnect if the connection closes
connection.on('close', (err) => {
this._currentConnection = undefined;
this.emit('disconnect', { err });
const handle = wait(this.reconnectTimeInSeconds * 1000);
this._cancelRetriesHandler = handle.cancel;
handle.promise
.then(() => this._connect())
// `_connect()` should never throw.
.catch(neverThrows);
});
this._connectPromise = undefined;
this.emit('connect', { connection, url: originalUrl });
// Need to return null here, or Bluebird will complain - #171.
return null;
});
})
.catch((err) => {
this.emit('connectFailed', { err, url: attemptedUrl });
// Connection failed...
this._currentConnection = undefined;
this._connectPromise = undefined;
let handle;
if (err.name === 'OperationalError' && err.message === 'connect ETIMEDOUT') {
handle = wait(0);
} else {
handle = wait(this.reconnectTimeInSeconds * 1000);
}
this._cancelRetriesHandler = handle.cancel;
return handle.promise.then(() => this._connect());
}));
return result;
}
}