-
-
Notifications
You must be signed in to change notification settings - Fork 577
/
amqp.js
415 lines (372 loc) · 12 KB
/
amqp.js
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
/*
* moleculer
* Copyright (c) 2017 Ice Services (https://github.com/ice-services/moleculer)
* MIT Licensed
*/
"use strict";
const Promise = require("bluebird");
const _ = require("lodash");
const Transporter = require("./base");
const {
PACKET_REQUEST,
PACKET_RESPONSE,
PACKET_UNKNOW,
PACKET_EVENT,
PACKET_DISCOVER,
PACKET_INFO,
PACKET_DISCONNECT,
PACKET_HEARTBEAT,
PACKET_PING,
PACKET_PONG,
} = require("../packets");
/**
* Transporter for AMQP
*
* More info: https://www.amqp.org/
*
* For test:
*
* docker run -d -p 5672:5672 -p 15672:15672 --name rabbit rabbitmq:3-management
*
* @class AmqpTransporter
* @extends {Transporter}
*/
class AmqpTransporter extends Transporter {
/**
* Creates an instance of AmqpTransporter.
*
* @param {any} opts
*
* @memberOf AmqpTransporter
*/
constructor(opts) {
if (typeof opts === "string")
opts = { amqp: { url: opts } };
// Number of requests a broker will handle concurrently
if (typeof opts.amqp.prefetch !== "number")
opts.amqp.prefetch = 1;
// Number of milliseconds before an event expires
if (typeof opts.amqp.eventTimeToLive !== "number")
opts.amqp.eventTimeToLive = 5000;
if (typeof opts.amqp.queueOptions !== "object")
opts.amqp.queueOptions = {};
if (typeof opts.amqp.exchangeOptions !== "object")
opts.amqp.exchangeOptions = {};
if (typeof opts.amqp.messageOptions !== "object")
opts.amqp.messageOptions = {};
if (typeof opts.amqp.consumeOptions !== "object")
opts.amqp.consumeOptions = {};
super(opts);
this.hasBuiltInBalancer = true;
this.connection = null;
this.channel = null;
this.bindings = [];
}
/**
* Connect to a AMQP server
*
* @memberOf AmqpTransporter
*/
connect() {
return new Promise((resolve, reject) => {
let amqp;
try {
amqp = require("amqplib");
} catch(err) {
/* istanbul ignore next */
this.broker.fatal("The 'amqplib' package is missing! Please install it with 'npm install amqplib --save' command!", err, true);
}
amqp.connect(this.opts.amqp.url)
.then(connection => {
this.connection = connection;
this.logger.info("AMQP connected!");
/* istanbul ignore next*/
connection
.on("error", (err) => {
this.connected = false;
reject(err);
this.logger.error("AMQP connection error!");
})
.on("close", (err) => {
this.connected = false;
reject(err);
this.logger.error("AMQP connection closed!");
})
.on("blocked", (reason) => {
this.logger.warn("AMQP connection blocked!", reason);
})
.on("unblocked", () => {
this.logger.info("AMQP connection unblocked!");
});
connection
.createChannel()
.then((channel) => {
this.channel = channel;
this.onConnected().then(resolve);
this.logger.info("AMQP channel created!");
channel.prefetch(this.opts.amqp.prefetch);
/* istanbul ignore next*/
channel
.on("close", () => {
this.connected = false;
this.channel = null;
reject();
this.logger.warn("AMQP channel closed!");
})
.on("error", (err) => {
this.connected = false;
reject(err);
this.logger.error("AMQP channel error!", err);
})
.on("drain", () => {
this.logger.info("AMQP channel drained!");
})
.on("return", (msg) => {
this.logger.info("AMQP channel returned a message!", msg);
});
})
.catch((err) => {
/* istanbul ignore next*/
this.logger.error("AMQP failed to create channel!");
this.connected = false;
reject(err);
});
})
.catch((err) => {
/* istanbul ignore next*/
this.logger.warn("AMQP failed to connect!");
this.connected = false;
reject(err);
});
});
}
/**
* Disconnect from an AMQP server
*
* @memberOf AmqpTransporter
* @description Close the connection and unbind this node's queues.
* This prevents messages from being broadcasted to a dead node.
* Note: Some methods of ending a node process don't allow disconnect to fire, meaning that
* some dead nodes could still receive published packets.
* Queues and Exchanges are not be deleted since they could contain important messages.
*/
disconnect() {
if (this.connection && this.channel && this.bindings) {
return Promise.all(this.bindings.map(binding => this.channel.unbindQueue(...binding)))
.then(() => this.channel.close())
.then(() => this.connection.close())
.then(() => {
this.bindings = null;
this.channel = null;
this.connection = null;
})
.catch(err => this.logger.warn(err));
}
}
/**
* Get assertQueue options by packet type.
*
* @param {String} packetType
*
* @memberOf AmqpTransporter
*/
_getQueueOptions(packetType) {
let packetOptions;
switch(packetType) {
// Requests and responses don't expire.
case PACKET_REQUEST:
case PACKET_RESPONSE:
packetOptions = {};
break;
// Packet types meant for internal use will expire after 5 seconds.
case PACKET_DISCOVER:
case PACKET_DISCONNECT:
case PACKET_UNKNOW:
case PACKET_INFO:
case PACKET_HEARTBEAT:
case PACKET_PING:
case PACKET_PONG:
packetOptions = { messageTtl: 5000, autoDelete: true };
break;
// Consumers can decide how long events live. Defaults to 5 seconds.
case PACKET_EVENT:
packetOptions = { messageTtl: this.opts.amqp.eventTimeToLive, autoDelete: true };
break;
// Load-balanced/grouped events
case PACKET_EVENT + "LB":
packetOptions = {};
break;
}
return Object.assign(packetOptions, this.opts.amqp.queueOptions);
}
/**
* Build a function to handle requests.
*
* @param {String} cmd
* @param {Boolean} needAck
*
* @memberOf AmqpTransporter
*/
_consumeCB(cmd, needAck = false) {
return (msg) => {
const result = this.messageHandler(cmd, msg.content);
// If a promise is returned, acknowledge the message after it has resolved.
// This means that if a worker dies after receiving a message but before responding, the
// message won't be lost and it can be retried.
if(needAck) {
if (result instanceof Promise) {
return result.then(() => this.channel.ack(msg));
} else {
this.channel.ack(msg);
}
}
};
}
/**
* Subscribe to a command
*
* @param {String} cmd
* @param {String} nodeID
*
* @memberOf AmqpTransporter
* @description Initialize queues and exchanges for all packet types except Request.
*
* All packets that should reach multiple nodes have a dedicated qeuue per node, and a single
* exchange that routes each message to all queues. These packet types will not use
* acknowledgements and have a set time-to-live. The time-to-live for EVENT packets can be
* configured in options.
* Examples: INFO (sometimes), DISCOVER, DISCONNECT, HEARTBEAT, PING, PONG, EVENT
*
* Other Packets are headed towards a specific node or queue. These don't need exchanges and
* packets of this type will not expire.
* Examples: REQUEST, RESPONSE
*
* RESPONSE: Each node has its own dedicated queue and acknowledgements will not be used.
*
* REQUEST: Each action has its own dedicated queue. This way if an action has multiple workers,
* they can all pull from the same qeuue. This allows a message to be retried by a different node
* if one dies before responding.
*
* Note: Queue's for REQUEST packet types are not initialized in the subscribe method because the
* actions themselves are not available from within the method. Instead they are intercepted from
* "prefix.INFO" packets because they are broadcast whenever a service is registered.
*
*/
subscribe(cmd, nodeID) {
if (!this.channel) return;
const topic = this.getTopicName(cmd, nodeID);
// Some topics are specific to this node already, in these cases we don't need an exchange.
if (nodeID != null) {
return this.channel.assertQueue(topic, this._getQueueOptions(cmd))
.then(() => this.channel.consume(
topic,
this._consumeCB(cmd),
Object.assign({ noAck: true }, this.opts.amqp.consumeOptions)
));
} else {
// Create a queue specific to this nodeID so that this node can receive broadcasted messages.
const queueName = `${this.prefix}.${cmd}.${this.nodeID}`;
// Save binding arguments for easy unbinding later.
const bindingArgs = [queueName, topic, ""];
this.bindings.push(bindingArgs);
return Promise.all([
this.channel.assertExchange(topic, "fanout", this.opts.amqp.exchangeOptions),
this.channel.assertQueue(queueName, this._getQueueOptions(cmd)),
])
.then(() => Promise.all([
this.channel.bindQueue(...bindingArgs),
this.channel.consume(
queueName,
this._consumeCB(cmd),
Object.assign({ noAck: true }, this.opts.amqp.consumeOptions)
)
]));
}
}
/**
* Initialize queues for REQUEST packets.
*
* @memberOf AmqpTransporter
*/
_makeServiceSpecificSubscriptions() {
const services = this.broker.getLocalNodeInfo().services;
return Promise.all(services.map(service => {
if (typeof service.actions !== "object" && typeof service.events !== "object") return Promise.resolve();
const p = [];
if (service.actions) {
// Service actions queues
p.push(Object.keys(service.actions).map(action => {
const queue = `${this.prefix}.${PACKET_REQUEST}B.${action}`;
return this.channel.assertQueue(queue, this._getQueueOptions(PACKET_REQUEST))
.then(() => this.channel.consume(
queue,
this._consumeCB(PACKET_REQUEST, true),
this.opts.amqp.consumeOptions
));
}));
}
if (service.events) {
// Load-balanced/grouped events queues
p.push(Object.keys(service.events).map(event => {
const group = service.events[event].group || service.name;
const queue = `${this.prefix}.${PACKET_EVENT}B.${group}.${event}`;
return this.channel.assertQueue(queue, this._getQueueOptions(PACKET_EVENT + "LB"))
.then(() => this.channel.consume(
queue,
this._consumeCB(PACKET_EVENT, true),
this.opts.amqp.consumeOptions
));
}));
}
return Promise.all(_.compact(_.flatten(p, true)));
}));
}
/**
* Publish a packet
*
* @param {Packet} packet
*
* @memberOf AmqpTransporter
* @description Send packets to their intended queues / exchanges.
*
* Reasonings documented in the subscribe method.
*/
publish(packet) {
if (!this.channel) return Promise.resolve();
let topic = this.getTopicName(packet.type, packet.target);
if (packet.type === PACKET_EVENT && !packet.target && packet.payload.groups) {
let groups = packet.payload.groups;
// If the packet contains groups, we don't send the packet to
// the targetted node, but we push them to the event group queues
// and AMQP will load-balanced it.
if (groups.length > 0) {
groups.forEach(group => {
let queue = `${this.prefix}.${PACKET_EVENT}B.${group}.${packet.payload.event}`;
// Change the groups to this group to avoid multi handling in consumers.
packet.payload.groups = [group];
this.channel.sendToQueue(queue, Buffer.from(packet.serialize()), this.opts.amqp.messageOptions);
});
return Promise.resolve();
}
// If it's not contain, then it is a broadcasted event,
// we sent it in the normal way (exchange)
}
const payload = Buffer.from(packet.serialize()); // amqp.node expects data to be a buffer
if (packet.type === PACKET_REQUEST && packet.target == null) {
topic = `${this.prefix}.${PACKET_REQUEST}B.${packet.payload.action}`;
this.channel.sendToQueue(topic, payload, this.opts.amqp.messageOptions);
return Promise.resolve();
}
if (packet.target != null) {
this.channel.sendToQueue(topic, payload, this.opts.amqp.messageOptions);
} else {
this.channel.publish(topic, "", payload, this.opts.amqp.messageOptions);
}
// HACK: This is the best way I have found to obtain the broker's services.
if (packet.type === PACKET_INFO && packet.target == null) {
return this._makeServiceSpecificSubscriptions();
}
return Promise.resolve();
}
}
module.exports = AmqpTransporter;