-
Notifications
You must be signed in to change notification settings - Fork 1
/
producer.js
305 lines (263 loc) · 10.3 KB
/
producer.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
const utils = require('./utils');
const parsers = require('./message-parsers');
const { ARNAVMQ_MSG_TIMEOUT_DEPRECATED } = require('./warnings');
const ERRORS = {
TIMEOUT: 'Timeout reached',
};
const loggerAlias = 'arnav_mq:producer';
class ProducerError extends Error {
constructor({ name, message }) {
super(message);
this.name = name;
this.message = message;
Error.captureStackTrace(this, this.constructor);
}
}
class Producer {
constructor(connection) {
this.amqpRPCQueues = {};
this._connection = connection;
this.channel = null;
}
set connection(value) {
this._connection = value;
}
get connection() {
return this._connection;
}
/**
* Get a function to execute on channel consumer incoming message is received
* @param {string} queue name of the queue where messages are SENT
* @return {function} function executed by an amqp.node channel consume callback method
*/
maybeAnswer(queue) {
const rpcQueue = this.amqpRPCQueues[queue];
return (msg) => {
// check the correlation ID sent by the initial message using RPC
const { correlationId } = msg.properties;
const responsePromise = rpcQueue[correlationId];
if (responsePromise === undefined) {
const error = new Error(`Receiving RPC message from previous session: callback no more in memory. ${queue}`);
this._connection.config.transport.warn(
loggerAlias,
error
);
this._connection.config.logger.warn({
message: `${loggerAlias} ${error.message}`,
error,
params: { queue, rpcQueue }
});
return;
}
// if we found one, we execute the callback and delete it because it will never be received again anyway
this._connection.config.transport.info(
loggerAlias,
`[${queue}] < answer`
);
this._connection.config.logger.debug({
message: `${loggerAlias} [${queue}] < answer`,
params: { queue }
});
try {
responsePromise.resolve(parsers.in(msg));
} catch (e) {
responsePromise.reject(new ProducerError(e));
} finally {
delete rpcQueue[correlationId];
}
};
}
/**
* Create a RPC-ready queue
* @param {string} queue the queue name in which we send a RPC request
* @return {Promise} Resolves when answer response queue is ready to receive messages
*/
createRpcQueue(queue) {
this.amqpRPCQueues[queue] = this.amqpRPCQueues[queue] || {};
const rpcQueue = this.amqpRPCQueues[queue];
if (rpcQueue.queue) return Promise.resolve(rpcQueue.queue);
// we create the callback queue using base queue name + appending config hostname and :res for clarity
// ie. if hostname is gateway-http and queue is service-oauth, response queue will be service-oauth:gateway-http:res
// it is important to have different hostname or no hostname on each module sending message or there will be conflicts
const resQueue = `${queue}:${this._connection.config.hostname}:${process.pid}:res`;
rpcQueue.queue = this._connection
.get()
.then((channel) => channel
.assertQueue(resQueue, {
durable: true,
exclusive: true,
})
.then((q) => {
rpcQueue.queue = q.queue;
// if channel is closed, we want to make sure we cleanup the queue so future calls will recreate it
this._connection.addListener('close', () => {
delete rpcQueue.queue;
this.createRpcQueue(queue);
});
return channel.consume(q.queue, this.maybeAnswer(queue), {
noAck: true,
});
})
.then(() => rpcQueue.queue))
.catch(() => {
delete rpcQueue.queue;
return utils
.timeoutPromise(this._connection.config.timeout)
.then(() => this.createRpcQueue(queue));
});
return rpcQueue.queue;
}
publishOrSendToQueue(queue, msg, options) {
if (!options.routingKey) {
return this.channel.sendToQueue(queue, msg, options);
}
return this.channel.publish(queue, options.routingKey, msg, options);
}
/**
* Start a timer to reject the pending RPC call if no answer is received within the given timeout
* @param {string} queue The queue where the RPC request was sent
* @param {string} corrId The RPC correlation ID
* @param {number} time The timeout in ms to wait for an answer before triggering the rejection
* @return {void} Nothing
*/
prepareTimeoutRpc(queue, corrId, time) {
const producer = this;
setTimeout(() => {
const rpcCallback = producer.amqpRPCQueues[queue][corrId];
if (rpcCallback) {
rpcCallback.reject(new Error(ERRORS.TIMEOUT));
delete producer.amqpRPCQueues[queue][corrId];
}
}, time);
}
/**
* Send message with or without rpc protocol, and check if RPC queues are created
* @param {string} queue the queue to send `msg` on
* @param {any} msg string, object, number.. anything bufferable/serializable
* @param {object} options contain rpc property (if true, enable rpc for this message)
* @return {Promise} Resolves when message is correctly sent, or when response is received when rpc is enabled
*/
checkRpc(queue, msg, options) {
// messages are persistent
options.persistent = true;
if (options.rpc) {
return this.createRpcQueue(queue).then(() => {
// generates a correlationId (random uuid) so we know which callback to execute on received response
const corrId = utils.uuidV4();
options.correlationId = corrId;
// reply to us if you receive this message!
options.replyTo = this.amqpRPCQueues[queue].queue;
// convert timeout to amqp's expiration. It's message-level expiration.
// The message will be discarded from a queue once it’s been there longer than the given number of milliseconds
// This is needed to avoid the case when the message which is already expired from caller's point of view (via timeout)
// is still waiting in the queue and thus is about to be processed by the consumer.
// Unfortunately, we can do nothing if the message is already consumed and is being processed at the moment
// when the timeout appears.
if (options.timeout && options.timeout > 0) {
utils.emitWarn(ARNAVMQ_MSG_TIMEOUT_DEPRECATED);
options.expiration = options.timeout;
}
// set expiration if it isn't set yet
if (!options.expiration && this._connection.config.rpcTimeout > 0) {
options.expiration = this._connection.config.rpcTimeout;
}
this.publishOrSendToQueue(queue, msg, options);
// defered promise that will resolve when response is received
const prCallbacks = {};
const promise = new Promise((resolve, reject) => {
prCallbacks.resolve = resolve;
prCallbacks.reject = reject;
});
this.amqpRPCQueues[queue][corrId] = prCallbacks;
// Using given timeout or default one
const timeout = options.expiration || 0;
if (timeout > 0) {
this.prepareTimeoutRpc(queue, corrId, timeout);
}
return promise;
});
}
return this.publishOrSendToQueue(queue, msg, options);
}
/**
* @deprecated Use publish instead
* Ensure channel exists and send message using `checkRpc`
* @param {string} queue The destination queue on which we want to send a message
* @param {any} msg Anything serializable/bufferable
* @param {object} options message options (persistent, durable, rpc, etc.)
* @return {Promise} checkRpc response
*/
/* eslint prefer-rest-params: off */
produce(queue, msg, options) {
return this.publish(queue, msg, options);
}
/**
* Ensure channel exists and send message using `checkRpc`
* @param {string} queue The destination queue on which we want to send a message
* @param {string|object} msg Anything serializable/bufferable
* @param {object} options message options (persistent, durable, rpc, etc.)
* @return {Promise} checkRpc response
*/
/* eslint no-param-reassign: "off" */
publish(queue, msg, options) {
// default options are persistent and durable because we do not want to miss any outgoing message
// unless user specify it
const settings = { persistent: true, durable: true, ...options };
let message = msg;
if (Array.isArray(msg)) {
message = Object.assign([], msg);
} else if (typeof msg !== 'string') {
message = { ...msg };
}
return this._sendToQueue(queue, message, settings, 0);
}
_sendToQueue(queue, message, settings, currentRetryNumber) {
return this._connection
.get()
.then((channel) => {
this.channel = channel;
// undefined can't be serialized/buffered :p
if (!message) message = null;
this._connection.config.transport.info(
loggerAlias,
`[${queue}] > `,
message
);
this._connection.config.logger.debug({
message: `${loggerAlias} [${queue}] > ${message}`,
params: { queue, message }
});
return this.checkRpc(queue, parsers.out(message, settings), settings);
})
.catch((error) => {
if (!this._shouldRetry(error, currentRetryNumber)) {
throw error;
}
// add timeout between retries because we don't want to overflow the CPU
this._connection.config.transport.error(loggerAlias, error);
this._connection.config.logger.error({
message: `${loggerAlias} Failed sending message to queue ${queue}: ${error.message}`,
error,
params: { queue, message }
});
return utils
.timeoutPromise(this._connection.config.timeout)
.then(() => this._sendToQueue(queue, message, settings, currentRetryNumber + 1));
});
}
_shouldRetry(error, currentRetryNumber) {
if (error instanceof ProducerError || error.message === ERRORS.TIMEOUT) {
return false;
}
const maxRetries = this._connection.config.producerMaxRetries;
if (maxRetries < 0) {
// Retry indefinitely...
return true;
}
return currentRetryNumber < maxRetries;
}
}
/* eslint no-unused-expressions: "off" */
/* eslint no-sequences: "off" */
/* eslint arrow-body-style: "off" */
module.exports = Producer;