-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrabbit.ts
327 lines (287 loc) · 8.85 KB
/
rabbit.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
import { Channel, Connection, Replies } from "amqplib";
import assert from "assert";
import util from "util";
import { ulid } from "ulid";
import { Logger, MessageHandler } from "./common";
import {
messagesReceivedCounter,
messagesFailedCounter,
messagesQueuedCounter,
messageHandlerDuration,
} from "./metrics";
export { registerMetrics } from "./metrics";
const noop = () => undefined;
export interface RabbitData<T> {
messageId: string;
routingKey: string;
timestamp: Date;
body: T;
}
export class RabbitHelper {
private amqpConn: Connection;
private logger: Logger;
private exchangeName: string;
// Tested having a channel pool, made no difference to performance
private useChan: Promise<Channel> | null = null;
constructor(opts: {
/** The amqplib connection */
amqpConnection: Connection;
/** The exchange name to publish to, defaults to "pubsub" */
exchangeName?: string;
/** Logger used for reporting errors */
logger: Logger;
}) {
const { exchangeName = "pubsub" } = opts;
this.amqpConn = opts.amqpConnection;
this.logger = opts.logger;
this.exchangeName = exchangeName;
}
/**
* onceListener returns a listener that will resolve message data once
*/
async onceListener<M>(args: {
/** The topic pattern to match */
topicPattern: string;
/** An optional signal use for aborting the operation */
signal?: AbortSignal;
}): Promise<{ data: () => Promise<RabbitData<M>> }> {
const ch = await this.amqpConn.createChannel();
try {
await ch.prefetch(1);
const q = await ch.assertQueue("", {
exclusive: true,
autoDelete: true,
durable: false,
});
await ch.bindQueue(q.queue, this.exchangeName, args.topicPattern);
let resolve: (data: RabbitData<M>) => void;
let reject: (err: unknown) => void = noop;
const dataP = new Promise<RabbitData<M>>((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
}).finally(() => {
ch.close();
});
assert(reject !== noop);
await ch.consume(
q.queue,
(msg) => {
if (!msg) {
reject(new Error("No message received"));
} else {
resolve({
messageId: msg.properties.messageId,
routingKey: msg.fields.routingKey,
timestamp: new Date(msg.properties.timestamp * 1000),
body: JSON.parse(msg.content.toString("utf8")),
});
}
},
{
// Don't think we need to ack for a once off
noAck: true,
exclusive: true,
}
);
if (args.signal) {
if (args.signal.aborted) {
reject(new Error("Aborted"));
} else {
args.signal.addEventListener(
"abort",
() => reject(new Error("Aborted")),
{ once: true }
);
}
}
return { data: () => dataP };
} catch (err) {
ch.close();
throw err;
}
}
async handleQueue<T>(args: {
queueName: string;
maxConcurrent?: number;
signal: AbortSignal;
handler: MessageHandler<RabbitData<T>>;
}): Promise<void> {
const inProgress = new Set<Promise<void>>();
const ch = await this.amqpConn.createChannel();
let chClosed = false;
const consumerAbort = new AbortController();
const onClose = () => {
chClosed = true;
consumerAbort.abort();
};
ch.once("close", onClose);
try {
await ch.prefetch(args.maxConcurrent || 20);
const { consumerTag } = await ch.consume(args.queueName, async (msg) => {
// msg is null when the consumer is cancelled
if (!msg) {
consumerAbort.abort();
return;
}
messagesReceivedCounter.inc({ queue: args.queueName });
const end = messageHandlerDuration.startTimer({
queue: args.queueName,
provider: "rabbit",
});
const death = msg.properties.headers?.["x-death"]?.find(
(d) => d.queue === args.queueName
);
// As far as I can tell, the TO routing key is always the first one, and
// the CC are the ones that follow
const routingKey = death
? death["routing-keys"][0]
: msg.fields.routingKey;
const task = args
.handler({
messageId: msg.properties.messageId,
routingKey,
timestamp: new Date(msg.properties.timestamp * 1000),
body: JSON.parse(msg.content.toString("utf8")),
})
.then(
() => ch.ack(msg),
(err: unknown) => {
messagesFailedCounter.inc({
queue: args.queueName,
provider: "rabbit",
});
this.logger.error(
util.format(
"Error handling message message_id=%j routing_key=%s deaths=%s: %s",
msg.properties.messageId,
routingKey,
death?.count ?? 0,
err
)
);
// TODO: when do we want to requeue? Nacking with requeue=false
// sends to dead letter queue
ch.nack(msg, false, false);
}
);
task.finally(() => {
end();
inProgress.delete(task);
});
inProgress.add(task);
});
const raceSignal = AbortSignal.any([args.signal, consumerAbort.signal]);
if (!raceSignal.aborted) {
await new Promise((resolve) => {
raceSignal.addEventListener("abort", resolve, {
once: true,
});
});
}
if (!chClosed) {
await ch.cancel(consumerTag);
await Promise.all(inProgress);
}
} finally {
ch.removeListener("close", onClose);
if (!chClosed) {
await ch.close();
}
}
}
async publish(topic: string, payload: unknown): Promise<void> {
return this.usingChannel(async (ch) => {
ch.publish(
this.exchangeName,
topic,
Buffer.from(JSON.stringify(payload)),
{
messageId: ulid(),
contentType: "application/json",
contentEncoding: "utf-8",
timestamp: unixTime(),
}
);
});
}
async sendToQueue(queue: string, payload: unknown) {
return this.usingChannel(async (ch) => {
ch.sendToQueue(queue, Buffer.from(JSON.stringify(payload)), {
messageId: ulid(),
contentType: "application/json",
contentEncoding: "utf-8",
timestamp: unixTime(),
});
messagesQueuedCounter.inc({ queue, provider: "rabbit" });
});
}
/**
* assets the topic exchange exists
*/
async assertExchange(opts?: { durable?: boolean }): Promise<void> {
return this.usingChannel(async (ch) => {
await ch.assertExchange(this.exchangeName, "topic", opts);
});
}
/**
* Asserts a work queue with a dead letter queue
* @param queueName The name of the queue to assert
* @param options.retryDelay The delay in milliseconds to wait before retrying
* a nacked message
*/
async assertWorkQueue(
queueName: string,
options: { retryDelay: number }
): Promise<Replies.AssertQueue> {
return this.usingChannel(async (ch) => {
if (options.retryDelay <= 0) {
throw new Error("retryDelay must be greater than 0");
}
const deadLetterQueue = `${queueName}-retry`;
await ch.assertQueue(deadLetterQueue, {
durable: true,
deadLetterExchange: "",
deadLetterRoutingKey: queueName,
messageTtl: options.retryDelay,
});
// Use the default exchange (empty string) to send messages directly to
// the queue
return await ch.assertQueue(queueName, {
durable: true,
deadLetterExchange: "",
deadLetterRoutingKey: deadLetterQueue,
});
});
}
async createSubscriptionQueue(): Promise<Replies.AssertQueue> {
return this.usingChannel(async (ch) => {
return await ch.assertQueue("", { exclusive: true, autoDelete: true });
});
}
async bindQueue(queueName: string, topicPattern: string): Promise<void> {
return this.usingChannel(async (ch) => {
await ch.bindQueue(queueName, this.exchangeName, topicPattern);
});
}
async unbindQueue(queueName: string, topicPattern: string): Promise<void> {
return this.usingChannel(async (ch) => {
await ch.unbindQueue(queueName, this.exchangeName, topicPattern);
});
}
async usingChannel<T>(fn: (ch: Channel) => Promise<T>): Promise<T> {
let ch: Channel;
if (!this.useChan) {
this.useChan = Promise.resolve(this.amqpConn.createChannel());
ch = await this.useChan;
ch.once("close", () => {
this.useChan = null;
});
} else {
ch = await this.useChan;
}
return await fn(ch);
}
}
function unixTime() {
return Math.floor(Date.now() / 1000);
}