-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
server-rmq.ts
253 lines (231 loc) · 7.5 KB
/
server-rmq.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
import {
isNil,
isString,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import {
CONNECTION_FAILED_MESSAGE,
CONNECT_EVENT,
CONNECT_FAILED_EVENT,
DISCONNECTED_RMQ_MESSAGE,
DISCONNECT_EVENT,
NO_MESSAGE_HANDLER,
RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT,
RQM_DEFAULT_NOACK,
RQM_DEFAULT_NO_ASSERT,
RQM_DEFAULT_PREFETCH_COUNT,
RQM_DEFAULT_QUEUE,
RQM_DEFAULT_QUEUE_OPTIONS,
RQM_DEFAULT_URL,
RQM_NO_EVENT_HANDLER,
RQM_NO_MESSAGE_HANDLER,
} from '../constants';
import { RmqContext } from '../ctx-host';
import { Transport } from '../enums';
import { RmqUrl } from '../external/rmq-url.interface';
import { CustomTransportStrategy, RmqOptions } from '../interfaces';
import {
IncomingRequest,
OutgoingResponse,
ReadPacket,
} from '../interfaces/packet.interface';
import { RmqRecordSerializer } from '../serializers/rmq-record.serializer';
import { Server } from './server';
let rmqPackage: any = {};
const INFINITE_CONNECTION_ATTEMPTS = -1;
export class ServerRMQ extends Server implements CustomTransportStrategy {
public readonly transportId = Transport.RMQ;
protected server: any = null;
protected channel: any = null;
protected connectionAttempts = 0;
protected readonly urls: string[] | RmqUrl[];
protected readonly queue: string;
protected readonly prefetchCount: number;
protected readonly noAck: boolean;
protected readonly queueOptions: any;
protected readonly isGlobalPrefetchCount: boolean;
protected readonly noAssert: boolean;
constructor(protected readonly options: RmqOptions['options']) {
super();
this.urls = this.getOptionsProp(this.options, 'urls') || [RQM_DEFAULT_URL];
this.queue =
this.getOptionsProp(this.options, 'queue') || RQM_DEFAULT_QUEUE;
this.prefetchCount =
this.getOptionsProp(this.options, 'prefetchCount') ||
RQM_DEFAULT_PREFETCH_COUNT;
this.noAck = this.getOptionsProp(this.options, 'noAck', RQM_DEFAULT_NOACK);
this.isGlobalPrefetchCount =
this.getOptionsProp(this.options, 'isGlobalPrefetchCount') ||
RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT;
this.queueOptions =
this.getOptionsProp(this.options, 'queueOptions') ||
RQM_DEFAULT_QUEUE_OPTIONS;
this.noAssert =
this.getOptionsProp(this.options, 'noAssert') || RQM_DEFAULT_NO_ASSERT;
this.loadPackage('amqplib', ServerRMQ.name, () => require('amqplib'));
rmqPackage = this.loadPackage(
'amqp-connection-manager',
ServerRMQ.name,
() => require('amqp-connection-manager'),
);
this.initializeSerializer(options);
this.initializeDeserializer(options);
}
public async listen(
callback: (err?: unknown, ...optionalParams: unknown[]) => void,
): Promise<void> {
try {
await this.start(callback);
} catch (err) {
callback(err);
}
}
public close(): void {
this.channel && this.channel.close();
this.server && this.server.close();
}
public async start(
callback?: (err?: unknown, ...optionalParams: unknown[]) => void,
) {
this.server = this.createClient();
this.server.on(CONNECT_EVENT, () => {
if (this.channel) {
return;
}
this.channel = this.server.createChannel({
json: false,
setup: (channel: any) => this.setupChannel(channel, callback),
});
});
const maxConnectionAttempts = this.getOptionsProp(
this.options,
'maxConnectionAttempts',
INFINITE_CONNECTION_ATTEMPTS,
);
this.server.on(DISCONNECT_EVENT, (err: any) => {
this.logger.error(DISCONNECTED_RMQ_MESSAGE);
this.logger.error(err);
});
this.server.on(CONNECT_FAILED_EVENT, (error: Record<string, unknown>) => {
this.logger.error(CONNECTION_FAILED_MESSAGE);
if (error?.err) {
this.logger.error(error.err);
}
const isReconnecting = !!this.channel;
if (
maxConnectionAttempts === INFINITE_CONNECTION_ATTEMPTS ||
isReconnecting
) {
return;
}
if (++this.connectionAttempts === maxConnectionAttempts) {
this.close();
callback?.(error.err ?? new Error(CONNECTION_FAILED_MESSAGE));
}
});
}
public createClient<T = any>(): T {
const socketOptions = this.getOptionsProp(this.options, 'socketOptions');
return rmqPackage.connect(this.urls, {
connectionOptions: socketOptions,
heartbeatIntervalInSeconds: socketOptions?.heartbeatIntervalInSeconds,
reconnectTimeInSeconds: socketOptions?.reconnectTimeInSeconds,
});
}
public async setupChannel(channel: any, callback: Function) {
if (!this.queueOptions.noAssert) {
await channel.assertQueue(this.queue, this.queueOptions);
}
await channel.prefetch(this.prefetchCount, this.isGlobalPrefetchCount);
channel.consume(
this.queue,
(msg: Record<string, any>) => this.handleMessage(msg, channel),
{
noAck: this.noAck,
consumerTag: this.getOptionsProp(
this.options,
'consumerTag',
undefined,
),
},
);
callback();
}
public async handleMessage(
message: Record<string, any>,
channel: any,
): Promise<void> {
if (isNil(message)) {
return;
}
const { content, properties } = message;
const rawMessage = this.parseMessageContent(content);
const packet = await this.deserializer.deserialize(rawMessage, properties);
const pattern = isString(packet.pattern)
? packet.pattern
: JSON.stringify(packet.pattern);
const rmqContext = new RmqContext([message, channel, pattern]);
if (isUndefined((packet as IncomingRequest).id)) {
return this.handleEvent(pattern, packet, rmqContext);
}
const handler = this.getHandlerByPattern(pattern);
if (!handler) {
if (!this.noAck) {
this.logger.warn(RQM_NO_MESSAGE_HANDLER`${pattern}`);
this.channel.nack(rmqContext.getMessage(), false, false);
}
const status = 'error';
const noHandlerPacket = {
id: (packet as IncomingRequest).id,
err: NO_MESSAGE_HANDLER,
status,
};
return this.sendMessage(
noHandlerPacket,
properties.replyTo,
properties.correlationId,
);
}
const response$ = this.transformToObservable(
await handler(packet.data, rmqContext),
);
const publish = <T>(data: T) =>
this.sendMessage(data, properties.replyTo, properties.correlationId);
response$ && this.send(response$, publish);
}
public async handleEvent(
pattern: string,
packet: ReadPacket,
context: RmqContext,
): Promise<any> {
const handler = this.getHandlerByPattern(pattern);
if (!handler && !this.noAck) {
this.channel.nack(context.getMessage(), false, false);
return this.logger.warn(RQM_NO_EVENT_HANDLER`${pattern}`);
}
return super.handleEvent(pattern, packet, context);
}
public sendMessage<T = any>(
message: T,
replyTo: any,
correlationId: string,
): void {
const outgoingResponse = this.serializer.serialize(
message as unknown as OutgoingResponse,
);
const options = outgoingResponse.options;
delete outgoingResponse.options;
const buffer = Buffer.from(JSON.stringify(outgoingResponse));
this.channel.sendToQueue(replyTo, buffer, { correlationId, ...options });
}
protected initializeSerializer(options: RmqOptions['options']) {
this.serializer = options?.serializer ?? new RmqRecordSerializer();
}
private parseMessageContent(content: Buffer) {
try {
return JSON.parse(content.toString());
} catch {
return content.toString();
}
}
}