-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
657 lines (547 loc) · 21.1 KB
/
index.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import * as amqplib from 'amqplib';
// @ts-ignore
import { cyan, red, yellow } from 'chalk';
import { v4 } from 'node-uuid';
import { fromCallback } from 'promise-cb';
import { initWhitelist, log, shouldSkipSubscribe } from './lib/debug';
import { parse, parseError, stringify, stringifyError } from './lib/serialization';
import { wait } from './lib/wait';
const DEFAULT_RPC_EXPIRY = 15000; // 15 seconds
const BACKOFF_EXPIRY_MS = 2000; // we use a fixed backoff expiry for now of 2 seconds
function maybeFromCallback<T>(fn: ((done: Rabbitr.Callback<T>) => void) | (() => PromiseLike<T>)): Promise<T> {
let callback: Rabbitr.Callback<T>;
let promise = fromCallback<T>(_callback => (callback = _callback) && void 0);
// @ts-ignore
let val = (fn as Function)(callback);
if (val && val.then) {
return Promise.resolve<T>(val);
}
return promise;
}
class TimeoutError extends Error {
topic: string;
isRpc: boolean;
name = 'TimeoutError';
constructor(details: { isRpc: boolean, topic: string }) {
super();
this.isRpc = details.isRpc;
this.topic = details.topic;
this.message = `${details.isRpc ? 'RPC request ' : ''}timed out on topic ${this.topic}`;
(Error as any).captureStackTrace(this, this.constructor)
}
}
class Rabbitr {
opts: Rabbitr.IOptions;
/** @deprecated */
protected ready = false;
/** @deprecated */
protected connected = false;
protected connection?: amqplib.Connection;
protected log = log;
private _openChannels: amqplib.Channel[];
private _timerChannel?: amqplib.Channel;
private _backoffChannel?: amqplib.Channel;
private _publishChannel?: amqplib.Channel;
private _rpcReturnChannel?: amqplib.Channel;
_cachedChannel?: amqplib.Channel;
private connectionPromise: Promise<amqplib.Connection>;
private isShuttingDown: boolean = false;
private pendingMessagesCount: number = 0;
private shutdown = () => {
this.isShuttingDown = true;
this.log(`${red('shutting down')}`);
return this.postMessage();
};
private postMessage() {
if (this.isShuttingDown) {
if (this.pendingMessagesCount) {
// wait
this.log(`we have ${yellow(this.pendingMessagesCount + '')} pending messsges`);
}
}
}
/**
* An array of channel names used for debug mode. If this value is set, calls to
* #subscribe on channels not in this list will be ignored.
*
* This option is set from the environment variable `RABBITR_DEBUG`.
*/
private debugChannelsWhitelist: string[] | null;
constructor(opts: Rabbitr.IOptions) {
this.debugChannelsWhitelist = initWhitelist();
this.opts = Object.assign({
url: '',
defaultRPCExpiry: DEFAULT_RPC_EXPIRY,
}, opts);
this.opts.connectionOpts = Object.assign({
heartbeat: 1
}, opts && opts.connectionOpts || {});
// istanbul ignore next
if (!this.opts.url) {
throw new Error('Missing `url` in Rabbitr options');
}
if (this.opts.log) {
this.log = this.opts.log;
}
this._openChannels = [];
// begin to initiate the connection now
this.log('#connect');
this.log(`using connection url ${yellow(this.opts.url)}`);
this.connectionPromise = this.initiateConnection();
this.connectionPromise.catch((err) => {
console.error('failed to connect to rabbit');
console.error(err);
process.exit(1);
});
}
private async initiateConnection(): Promise<amqplib.Connection> {
// start by opening an AMQP connection
this.connection = await amqplib.connect(this.opts.url, this.opts.connectionOpts);
// make sure to close the connection if the process terminates
process.once('SIGINT', this.shutdown);
process.once('SIGTERM', this.shutdown);
this.connection.on('close', () => {
process.removeListener('SIGINT', this.shutdown);
process.removeListener('SIGTERM', this.shutdown);
if (!this.isShuttingDown) {
throw new Error('Disconnected from RabbitMQ');
}
this.log(`connection closed`);
});
// now setup all the channels necessary for standard operation
const channels = await Promise.all([
this.connection.createChannel(),
this.connection.createChannel(),
]);
this._timerChannel = channels[0];
this._publishChannel = channels[0];
this._backoffChannel = channels[0];
this._cachedChannel = channels[0];
this._rpcReturnChannel = channels[1];
this._openChannels = this._openChannels.concat(channels);
// mark the connection as open and perform any 'setup' method if one is set
this.connected = true;
// mark this instance as 'ready' and end the promise
this.ready = true;
return this.connection;
}
/** method to destroy anything for this instance of rabbitr */
async destroy() {
if(!this.connected) {
this.log(`${red('unable to destroy this instance as it is not connected')}`);
return;
}
// mark this instance as no longer valid
this.log(`${red('destroying')}`);
this.ready = false;
this.connected = false;
// close the connection
await this.connection?.close();
this.log('connection closed');
// @ts-ignore
this.connectionPromise = null;
// @ts-ignore
this.connection = null;
}
// standard pub/sub stuff
async send(topic: string, data: any, opts?: Rabbitr.ISendOptions): Promise<void>;
async send<TInput>(topic: string, data: TInput, opts?: Rabbitr.ISendOptions): Promise<void>;
async send<TInput>(topic: string, data: TInput, opts?: Rabbitr.ISendOptions): Promise<void> {
await this.connectionPromise;
this.log(yellow('send'), topic, data, opts);
// first ensure the exchange exists
await this._publishChannel?.assertExchange(topic, 'topic', {});
// then publish the message
const isSuccess = this._publishChannel?.publish(topic, '*', Buffer.from(stringify(data)), {
contentType: 'application/json',
});
if(!isSuccess) {
throw new Error(`Failed to publish to topic ${topic} with data ${data}`);
}
}
async subscribe(exchangeNames: string[], queueName: string, opts: Rabbitr.ISubscribeOptions, executorFunc: (data: Rabbitr.IMessage<any>) => void): Promise<amqplib.Channel>;
async subscribe<TMessage>(exchangeNames: string[], queueName: string, opts: Rabbitr.ISubscribeOptions, executorFunc: (data: Rabbitr.IMessage<TMessage>) => void): Promise<amqplib.Channel>;
async subscribe<TMessage>(exchangeNames: string[], queueName: string, opts: Rabbitr.ISubscribeOptions, executorFunc: (data: Rabbitr.IMessage<TMessage>) => void): Promise<amqplib.Channel> {
await this.connectionPromise;
this.log(cyan('subscribe'), exchangeNames, queueName, opts);
// here we bind all the exchanges into the queues
await Promise.all(exchangeNames.map((exchangeName) => this._bindExchangeToQueue(exchangeName, queueName)));
if (
shouldSkipSubscribe(this.debugChannelsWhitelist, queueName)
) {
// this queueName should be skipped as it's not in the 'debug whitelist'
this.log(red('skipped'), cyan('subscribe'), queueName);
// @ts-ignore
return;
}
const backoffQueueName = `backoff.${queueName}`;
// first, open a unique connection for this subscription
const channel = await this.connection?.createChannel();
this._openChannels.push(channel!);
// ensure the queue we want to listen on actually exists
await channel!.assertQueue(queueName, Object.assign({
durable: true,
}, opts));
if (opts?.skipBackoff !== true) {
const requeueExchangeName = `requeue.${queueName}`;
// create the backoff queue
await channel?.assertQueue(backoffQueueName, {
durable: true,
deadLetterExchange: requeueExchangeName,
arguments: {
'x-dead-letter-routing-key': '*',
},
messageTtl: BACKOFF_EXPIRY_MS,
});
// create an exchange that allows requeing back to the main queue
await this._bindExchangeToQueue(requeueExchangeName, queueName);
}
// set the concurrency on the channel
channel?.prefetch(opts && opts.prefetch || 1);
// define the internal message processing handler
const processMessage = async (msg: any) => {
if (!msg) return;
if (this.isShuttingDown) {
this.log(`${red('rejected')} message on queueName ${yellow(queueName)} because we're shutting down`);
channel?.nack(msg);
return;
}
let data = msg.content.toString();
if (msg.properties.contentType === 'application/json') {
data = parse(data);
}
this.log(`got a new message on ${cyan(queueName)}`, data);
try {
++ this.pendingMessagesCount;
// we create a new promise, and make the resolution an 'ack' and the rejection an 'nack'
await new Promise<void>((ack: () => void, reject) => {
const message: Rabbitr.IMessage<TMessage> = {
send: this.send.bind(this),
// @ts-ignore
rpcExec: this.rpcExec.bind(this),
topic: queueName,
data,
channel: channel!,
ack,
reject,
properties: msg.properties,
};
executorFunc(message);
});
// if we hit here, we should ack
this.log(`acking on ${cyan(queueName)}`, data);
channel?.ack(msg);
}
catch(err) {
// if we hit here, we have an error
if (opts?.skipBackoff !== true) {
// backoff works by sending to a queue with an expiration
// which then routes back into our main queue
this.log(`backing off msg on ${cyan(queueName)}`, data);
await channel?.sendToQueue(backoffQueueName, msg.content, {
contentType: msg.properties.contentType,
});
// we have to ack this message to prevent a double redelivery
await channel?.ack(msg);
}
else {
//
this.log(`rejecting on ${cyan(queueName)}`, data);
channel?.nack(msg);
}
}
finally {
-- this.pendingMessagesCount;
this.postMessage();
}
};
// start consuming, and return the channel we opened for this subscription
await channel?.consume(queueName, processMessage, {});
return channel!;
}
private async _bindExchangeToQueue(exchange: string, queue: string): Promise<void> {
await this.connectionPromise;
this.log(cyan('bindExchangeToQueue'), exchange, queue);
const channel = await this.connection?.createChannel();
try {
// make sure the queue and exchange exist
// @ts-ignore
await channel.assertQueue(queue, null);
// @ts-ignore
await channel.assertExchange(exchange, 'topic', null);
// finally bind the queue + exchange together
await channel?.bindQueue(queue, exchange, '*', {});
}
catch(err) {
// simply rethrow, try catch is just for finally
throw err;
}
finally {
// make sure to close the channel we opened
await channel?.close();
}
}
// timed queue stuff
private _timerQueueName(topic: string, uniqueID: string): string {
return `dlq.${topic}.${uniqueID}`;
}
async setTimer<TData>(topic: string, uniqueID: string, data: TData, ttl: number): Promise<void> {
await this.connectionPromise;
this.log(yellow('setTimer'), topic, uniqueID, data);
// format the queue name
const timerQueue = this._timerQueueName(topic, uniqueID);
// create the timer queue if it doesn't already exist
await this._timerChannel?.assertQueue(timerQueue, {
durable: true,
deadLetterExchange: topic,
arguments: {
'x-dead-letter-routing-key': '*',
},
expires: (ttl + 1000),
});
// now send the message direct into the timer
this._timerChannel?.sendToQueue(timerQueue, Buffer.from(stringify(data)), {
contentType: 'application/json',
expiration: `${ttl}`,
});
}
async clearTimer(topic: string, uniqueID: string): Promise<void> {
await this.connectionPromise;
const timerQueue = this._timerQueueName(topic, uniqueID);
this.log(yellow('clearTimer'), timerQueue);
await this._timerChannel?.deleteQueue(timerQueue, {});
}
// rpc helper methods
private _rpcQueueName(topic: string): string {
return `rpc.${topic}`;
}
private async _createTempQueue(queueName: string, channel: amqplib.Channel) {
this.log(`creating temp queue ${cyan(queueName)}`);
await channel.assertQueue(queueName, {
exclusive: true,
// @ts-ignore
expires: (this.opts.defaultRPCExpiry * 1 + 1000),
durable: false,
});
}
async rpcExec(topic: string, data: any, opts?: Rabbitr.IRpcExecOptions): Promise<any>;
async rpcExec<TInput, TOutput>(topic: string, data: TInput, opts?: Rabbitr.IRpcExecOptions): Promise<TOutput>;
async rpcExec<TInput, TOutput>(topic: string, data: TInput, opts?: Rabbitr.IRpcExecOptions): Promise<TOutput> {
await this.connectionPromise;
// this will send the data down the topic and then open up a unique return queue
const rpcQueue = this._rpcQueueName(topic);
const unique = v4() + '_' + ((Math.round(new Date().getTime() / 1000) + '').substr(5));
const returnQueueName = `${rpcQueue}.return.${unique}`;
const now = new Date().getTime();
const channel = this._rpcReturnChannel;
const replyQueue = returnQueueName;
// used to determine whether we should ignore timeouts
let isCompleted = false;
let replyConsumerTag: string | null = null;
try {
// bind the response queue
await this._createTempQueue(replyQueue, channel!);
this.log(`using rpc return queue ${cyan(returnQueueName)}`);
const timeoutMS = opts && opts.timeout || this.opts.defaultRPCExpiry || DEFAULT_RPC_EXPIRY;
// define a promise that will start now, and collect the result that comes back on the response queue
const resultPromise = new Promise((completed: (responseData: TOutput) => void, failed) => {
// define a method that the #consume method will call
const gotReply = (msg?: any) => {
if (!msg) return;
this.log('got rpc reply', msg.content);
isCompleted = true;
// we got a reply, try and parse it
try {
let data: any = msg.content.toString();
if (msg.properties.contentType === 'application/json') {
data = parse(data);
}
let error = data.error;
const response: TOutput = data.response;
if (error) {
if (data.isError) {
throw parseError(error);
} else {
throw parse(error);
}
}
completed(response);
}
catch(err) {
// send the failure to the rejection of the promise
failed(err);
}
};
channel?.consume(replyQueue, gotReply, {noAck: true}).then((response) => {
replyConsumerTag = response.consumerTag;
}).catch((err) => {
failed(err);
});
});
// send the request now
const request = {
d: data,
returnQueue: replyQueue,
expiration: now + timeoutMS,
context: opts?.context,
};
this.log('sending rpc request');
this._publishChannel?.sendToQueue(rpcQueue, Buffer.from(stringify(request)), {
contentType: 'application/json',
expiration: `${timeoutMS}`,
});
// race the result against a timeout promise
// @ts-ignore
const result = await Promise.race<TOutput>([
resultPromise,
wait(timeoutMS).then(() => {
if (isCompleted) {
return null;
}
this.log(`request timeout firing for ${rpcQueue} to ${returnQueueName}`);
throw new TimeoutError({isRpc: true, topic});
})
]);
// @ts-ignore
return result;
}
catch(err) {
// rethrow, this try catch only exists for the finally
throw err;
}
finally {
// clean up our temp queue now we're done
channel?.deleteQueue(replyQueue, {}).catch((err) => {
this.log(`failed to remove temp queue ${replyQueue} due to error `, err);
});
if (replyConsumerTag) {
channel?.cancel(replyConsumerTag).catch((err) => {
this.log(`failed to cancel replyConsumerTag ${replyConsumerTag} due to error `, err);
});
}
}
}
async rpcListener(topic: string, opts: Rabbitr.IRpcListenerOptions<any, any>, executor: Rabbitr.IRpcListenerExecutor<any, any>): Promise<void>;
async rpcListener<TInput, TOutput>(topic: string, opts: Rabbitr.IRpcListenerOptions<TInput, TOutput>, executor: Rabbitr.IRpcListenerExecutor<TInput, TOutput>): Promise<void>;
async rpcListener<TInput, TOutput>(topic: string, opts: Rabbitr.IRpcListenerOptions<TInput, TOutput>, executor: Rabbitr.IRpcListenerExecutor<TInput, TOutput>): Promise<void> {
await this.connectionPromise;
const rpcQueueName = this._rpcQueueName(topic);
this.log(`has rpcListener for ${topic}`);
await this.subscribe(
[],
rpcQueueName,
Object.assign({}, opts, {
skipBackoff: true,
durable: false,
}),
async (envelope: Rabbitr.IEnvelopedMessage<TInput>) => {
// this method runs when we receive a new message
try {
const dataEnvelope = envelope.data;
const now = new Date().getTime();
if (now > dataEnvelope.expiration) {
// we consider the message as 'expired' if the current clock time is after the point at which we think the client would have fired their expiry promise
// NB - this means that clock drift between client + server can cause unwanted timeouts
envelope.ack();
return;
}
const message: Rabbitr.IMessageWithContext<TInput> = <Rabbitr.IEnvelopedMessage<TInput> & Rabbitr.IMessage<TInput>>envelope;
message.context = dataEnvelope.context;
message.data = dataEnvelope.d;
let responseData: any;
try {
// @ts-ignore
const response = await maybeFromCallback(executor.bind(null, message));
responseData = { response };
this.log(`${yellow('rpcListener')} responding to topic ${cyan(topic)} with`, response);
}
catch(err) {
this.log(`${yellow('rpcListener')} on topic ${cyan(topic)} ${red('hit error')}`, err);
const isError = err instanceof Error;
const errJSON = isError ?
stringifyError(err) :
stringify(err);
responseData = {
error: errJSON,
isError,
};
}
// ack here - this will get ignored if the executor has acked or nacked already anyway
message.ack();
// doesn't need wrapping in this.formatName as the rpcExec function already formats the return queue name as required
this._publishChannel?.sendToQueue(dataEnvelope.returnQueue, Buffer.from(stringify(responseData)), {
contentType: 'application/json',
});
}
catch(err) {
// something failed when processing the message, nack it
envelope.reject();
}
}
);
}
};
declare module Rabbitr {
/** you MUST provide a 'url' rather than separate 'host', 'password', 'vhost' now */
export interface IOptions {
url: string;
log?: (...args: string[]) => void;
connectionOpts?: {
heartbeat?: boolean;
};
defaultRPCExpiry?: number;
}
export interface ErrorCallback {
(err: Error): void;
}
export interface Callback<T> {
(err: Error): void;
(err: Error, data: T): void;
}
export interface IRpcExecOptions {
timeout?: number;
context?: any;
}
export interface IRpcListenerOptions<TInput, TOutput> {
prefetch?: number;
}
export type IRpcListenerExecutor<TInput, TOutput> =
((message: IMessage<TInput>) => PromiseLike<TOutput>) |
((message: IMessage<TInput>, respond: Callback<TOutput>) => void);
export interface ISubscribeOptions {
prefetch?: number;
skipBackoff?: boolean;
durable?: boolean;
}
export interface ISendOptions {
}
export interface IMessage<TData> {
ack(): void;
reject(error?: Error): void;
topic: string;
channel: amqplib.Channel;
data: TData;
properties?: any;
send(topic: string, data: any, cb?: Rabbitr.ErrorCallback, opts?: Rabbitr.ISendOptions): Promise<void>;
send<TInput>(topic: string, data: TInput, cb?: Rabbitr.ErrorCallback, opts?: Rabbitr.ISendOptions): Promise<void>;
rpcExec(topic: string, data: any, cb?: Rabbitr.Callback<any>): Promise<any>;
rpcExec(topic: string, data: any, opts: Rabbitr.IRpcExecOptions, cb?: Rabbitr.Callback<any>): Promise<any>;
rpcExec<TInput, TOutput>(topic: string, data: TInput, cb?: Rabbitr.Callback<TOutput>): Promise<TOutput>;
rpcExec<TInput, TOutput>(topic: string, data: TInput, opts: Rabbitr.IRpcExecOptions, cb?: Rabbitr.Callback<TOutput>): Promise<TOutput>;
queue?: {
shift: () => void;
};
}
export interface IMessageWithContext<TData> extends IMessage<TData> {
context?: string;
}
export interface IEnvelopedMessage<TData> extends IMessage<any> {
data: {
d: TData;
expiration: number;
returnQueue: string;
context?: any;
};
}
}
export = Rabbitr;