forked from segmentio/nsq.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriter.js
219 lines (172 loc) · 4.46 KB
/
writer.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
'use strict';
/**
* Module dependencies.
*/
const { EventEmitter } = require('node:events');
const Connection = require('./connection');
const debug = require('debug')('nsq:writer');
const healthCheck = require('./mixins/health-check');
const reconnect = require('./mixins/reconnect');
const utils = require('./utils');
class Writer extends EventEmitter {
/**
* Constructor.
*
* @param {String|Object} options - can be either the nsqd address string or an object with the following options
* @param {String[]} [options.nsqd] - nsqd addresses
* @param {Number} [options.maxConnectionAttempts=Infinity] - max reconnection attempts
* @param {Boolean} [options.healthCheck=false] - setup health check
* @api public
*/
constructor(options = {}) {
super();
// Coerce string option to an object option.
if (typeof options === 'string') {
options = { nsqd: [options] };
}
// Initialize properties.
this.maxConnectionAttempts = options.maxConnectionAttempts ?? Infinity;
this.nsqd = options.nsqd || [''];
this.conns = new Set();
this.healthCheck = options.healthCheck ?? false;
this.pendingConns = new Set();
this.publishQueue = [];
// Add health check mixin.
healthCheck(this);
this.connect();
this.n = 0;
}
/**
* Publish `msg` to `topic`.
*
* @param {String} topic
* @param {String|Buffer|Object|Array} msg
* @param {Function} [fn]
* @api public
*/
publish(topic, msg, fn) {
fn = fn || function() {};
// JSON support.
if (Array.isArray(msg)) {
msg = msg.map(coerce);
} else {
msg = coerce(msg);
}
if (this.conns.size) {
const conn = Array.from(this.conns)[this.n++ % this.conns.size];
// Publish message.
debug('%s - publish', conn.addr);
if (Array.isArray(msg)) {
conn.mpublish(topic, msg, fn);
} else {
conn.publish(topic, msg, fn);
}
} else {
// Wait for ready and retry.
this.publishQueue.push([topic, msg, fn]);
}
}
/**
* Establish connections to the given nsqd instances.
*
* @api private
*/
connect() {
for (const node of this.nsqd) {
this.connectTo(node);
}
}
/**
* Connect to nsqd at `addr`.
*
* @param {String} address
* @api private
*/
connectTo(address) {
debug('%s - connect', address);
const opts = utils.parseAddress(address);
const conn = new Connection(opts);
this.pendingConns.add(conn);
conn.on('close', () => {
this.conns.delete(conn);
if (!conn.closing) {
this.pendingConns.add(conn);
}
debug('%s - remove from pool (total: %s)', address, this.conns.size);
});
conn.on('ready', () => {
this.pendingConns.delete(conn);
this.conns.add(conn);
debug('%s - add to pool (total: %s)', address, this.conns.size);
if (this.publishQueue.length > 0) {
debug('draining messages from the publish queue (total: %s)', this.publishQueue.length);
for (const [topic, msg, fn] of this.publishQueue) {
this.publish(topic, msg, fn);
}
this.publishQueue = [];
}
});
// TODO: poll
// TODO: tests
// Apply reconnection mixin.
reconnect(conn, this.maxConnectionAttempts);
// Apply event delegation.
this.delegate(conn);
// Connect to the nsqd node.
conn.connect();
}
/**
* Delegate events from `conn`.
*
* @param {Connection} conn
* @api private
*/
delegate(conn) {
utils.delegate(conn, 'error response', this);
utils.delegate(conn, 'error', this);
utils.delegate(conn, 'connect', this);
utils.delegate(conn, 'ready', this);
utils.delegate(conn, 'end', this);
}
/**
* Close the connections.
*
* @param {Function} fn
* @api public
*/
close(fn) {
debug('close');
if (fn) {
this.once('close', fn);
}
this.pendingConns.forEach(conn => conn.destroy());
let n = this.conns.size;
if (n === 0) {
this.emit('close');
}
this.conns.forEach(conn => {
conn.end(() => {
debug('%s - conn ended', conn.addr);
if (--n === 0) {
this.emit('close');
}
});
});
}
}
/**
* Coerce `val`.
*
* @param {String|Object} val
* @returns {String}
*/
function coerce(val) {
if (val && typeof val === 'object' && !Buffer.isBuffer(val)) {
return JSON.stringify(val);
}
return val;
}
/**
* Expose `Writer`.
*/
module.exports = Writer;