-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsulKvMonitor.js
399 lines (332 loc) · 12.3 KB
/
ConsulKvMonitor.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
'use strict';
const EventEmitter = require('events');
const _ = require('lodash');
const AlreadyInitializedError = require('./Error').AlreadyInitializedError;
const WatchError = require('./Error').WatchError;
const WatchTimeoutError = require('./Error').WatchTimeoutError;
const kvFactory = require('./Factory');
const ConsulKvData = require('./ConsulKvData');
const DEFAULT_TIMEOUT_MSEC = 5000;
const HEALTH_FALLBACK_INTERVAL_MSEC = 1000;
const DEFAULT_RETRY_START_SERVICE_TIMEOUT_MSEC = 1000;
const X_CONSUL_HEADERS = ['x-consul-index', 'x-consul-knownleader', 'x-consul-lastcontact'];
/**
* @emits ConsulKvMonitor#changed
* @emits ConsulKvMonitor#error
* @emits ConsulKvMonitor#healthy
* @emits ConsulKvMonitor#unhealthy
*/
class ConsulKvMonitor extends EventEmitter {
/**
* @param {Object} options
* @param {String} options.keysPrefix - namespace in consul to monitor
* @param {Boolean} options.json - namespace in consul to monitor
* @param {Number} [options.timeoutMsec=5000] - connection timeout to consul
* @param {Consul} consul
* @throws {TypeError} On invalid options format
* @public
*/
constructor(options, consul) {
super();
if (!_.isPlainObject(options)) {
throw new TypeError('options must be an object');
}
if (!_.has(options, 'keysPrefix') || !_.isString(options.keysPrefix) || _.isEmpty(options.keysPrefix)) {
throw new TypeError('options.keysPrefix must be set and be a non-empty string');
}
if (!_.has(options, 'timeoutMsec')) {
this._timeoutMsec = DEFAULT_TIMEOUT_MSEC;
} else {
if (!_.isSafeInteger(options.timeoutMsec) || options.timeoutMsec <= 0) {
throw new TypeError('options.timeoutMsec must be a positive integer if set');
}
this._timeoutMsec = options.timeoutMsec;
}
// duck typing check
if (!_.isObject(consul) || !_.isFunction(consul.watch) ||
!_.isObject(consul.kv) || !_.isFunction(consul.kv.get)
) {
throw new TypeError('consul argument does not look like Consul object');
}
this._keysPrefix = options.keysPrefix;
this._json = options.json;
this._initialized = false;
this._consul = consul;
this._consulKvData = new ConsulKvData();
this._consulHeaders = {};
this._onWatcherChange = this._onWatcherChange.bind(this);
this._onWatcherError = this._onWatcherError.bind(this);
this._onWatcherEnd = this._onWatcherEnd.bind(this);
this._retryStartService = this._retryStartService.bind(this);
this._watchKvChange = null;
this._setWatchUnealthy();
this._setUninitialized();
this._fallbackToWatchHealthyInterval = null;
this._retryTimer = null;
}
isWatchHealthy() {
return this._isWatchHealthy;
}
_setWatchHealthy() {
this._isWatchHealthy = true;
}
_setWatchUnealthy() {
this._isWatchHealthy = false;
}
isInitialized() {
return this._initialized;
}
_setInitialized() {
this._initialized = true;
}
_setUninitialized() {
this._initialized = false;
}
_isWatcherRegistered() {
return this._watchKvChange !== null;
}
/**
* @returns {ConsulKvData}
*/
getData() {
return this._consulKvData;
}
/**
* @returns {Object}
*/
getConsulHeaders() {
return this._consulHeaders;
}
/**
* Starts service and resolves promise with initial list of KV data.
*
* Listens for changes after successful resolve.
*
* Promise will be rejected with:
* `AlreadyInitializedError` if service is already started.
* `WatchTimeoutError` if either initial data nor error received for 5000 msec
* `WatchError` on error from `consul` underlying method
*
* Rejection of promise means that watcher was stopped and no retries will be done.
*
* @returns {Promise.<Array>|Promise.<AlreadyInitializedError>|Promise.<WatchError>|Promise.<WatchTimeoutError>}
* @public
*/
start() {
if (this._isWatcherRegistered()) {
return Promise.reject(new AlreadyInitializedError('Service is already started'));
}
return this._registerWatcherAndWaitForInitialKvData()
.then(kvData => {
this._watchKvChange.on('change', this._onWatcherChange);
this._watchKvChange.on('error', this._onWatcherError);
this._watchKvChange.on('end', this._onWatcherEnd);
this._setInitialized();
this._setWatchHealthy();
this._consulKvData = kvData;
return kvData;
});
}
/**
* Stops service even if it is not started yet. Monitor becomes `uninitialized` and `unhealthy`.
*
* Does not listened for changes after execution.
*
* @returns {ConsulKvMonitor}
* @public
*/
stop() {
if (this._retryTimer !== null) {
clearTimeout(this._retryTimer);
this._retryTimer = null;
}
if (!this._isWatcherRegistered()) {
return this;
}
// we need to remove listener to prevent emitting of `end` event after stop of watcher
this._watchKvChange.removeListener('end', this._onWatcherEnd);
this._watchKvChange.end();
this._watchKvChange = null;
this._unsetFallbackToWatchHealthy();
this._setUninitialized();
this._setWatchUnealthy();
return this;
}
/**
* Registers `consul.watch` and assigns watcher to `this._watchKvChange` and waits for the
* first successful response from consul with list of KV data.
* On successful response resolves promise with array of kv records (it may be empty). Method doesn't
* add listener for `change` event.
*
* Promise will be rejected with:
* `AlreadyInitializedError` if another `consul.watch` execution is found.
* `WatchTimeoutError` if either initial data nor error received for 5000 msec
* `WatchError` on error from `consul` underlying method
*
* Rejection of promise means that watch was stopped and `this._watchKvChange` was cleared.
*
* @returns {Promise.<Array>|Promise.<AlreadyInitializedError>|Promise.<WatchError>|Promise.<WatchTimeoutError>}
* @private
*/
_registerWatcherAndWaitForInitialKvData() {
return new Promise((resolve, reject) => {
if (this._watchKvChange !== null) {
reject(new AlreadyInitializedError('Another `consul.watch` execution is found'));
}
this._watchKvChange = this._consul.watch({
method: this._consul.kv.get,
options: {
key: this._keysPrefix,
recurse: true,
wait: '60s',
},
});
const firstChange = (data, response) => {
this._watchKvChange.removeListener('error', firstError);
clearTimeout(timerId);
const {consulKvData, errors} = kvFactory.buildConsulKvData(data, this._json);
for (const headerName of X_CONSUL_HEADERS) {
this._consulHeaders[headerName] = response.headers[headerName];
}
if (!_.isEmpty(errors)) {
this._emitFactoryErrors(errors);
}
resolve(consulKvData);
};
const firstError = (err) => {
this._watchKvChange.removeListener('change', firstChange);
this._watchKvChange.end();
this._watchKvChange = null;
clearTimeout(timerId);
reject(new WatchError(err.message, {err}));
};
const timerId = setTimeout(() => {
this._watchKvChange.removeListener('error', firstError);
this._watchKvChange.removeListener('change', firstChange);
this._watchKvChange.end();
this._watchKvChange = null;
reject(new WatchTimeoutError('Initial consul watch request was timed out'));
}, this._timeoutMsec);
this._watchKvChange.once('change', firstChange);
this._watchKvChange.once('error', firstError);
});
}
/**
* This method receives list of key-value records sent by `consul.watch` in `consul` format. Performs
* parsing of response.
*
* If service was unhealthy, it becomes healthy.
*
* @param {Array} data - list of key-value records after some changes
* @param {IncomingMessage} response - response from Consul
* @emits ConsulKvMonitor#changed actual array of key-value records
* @private
*/
_onWatcherChange(data, response) {
let isHealthyStateChanged = false;
if (!this.isWatchHealthy()) {
this._setWatchHealthy();
isHealthyStateChanged = true;
}
const {consulKvData, errors} = kvFactory.buildConsulKvData(data, this._json);
this._consulKvData = consulKvData;
for (const headerName of X_CONSUL_HEADERS) {
this._consulHeaders[headerName] = response.headers[headerName];
}
if (isHealthyStateChanged) {
this.emit('healthy');
}
this.emit('changed', consulKvData);
if (!_.isEmpty(errors)) {
this._emitFactoryErrors(errors);
}
}
/**
* This method receives an Error from Consul.
*
* If service was healthy, it becomes unhealthy.
*
* @param {Error} err
* @emits ConsulKvMonitor#error `WatchError` error
* @private
*/
_onWatcherError(err) {
this._unsetFallbackToWatchHealthy();
if (this.isWatchHealthy()) {
this._setWatchUnealthy();
this.emit('unhealthy');
}
this._setFallbackToWatchHealthy();
this.emit('error', new WatchError(err.message, {err}));
}
/**
* This method is called when connection with Consul agent was refused.
*
* Monitor becomes `uninitialized` and `unhealthy`.
*
* @emits ConsulKvMonitor#emergencyStop
* @private
*/
async _onWatcherEnd() {
this._unsetFallbackToWatchHealthy();
this._setUninitialized();
if (this.isWatchHealthy()) {
this._setWatchUnealthy();
this.emit('unhealthy');
}
this._watchKvChange = null;
await this._retryStartService();
}
/**
* This method receives a list of errors and emits them.
*
* @param {Error[]} errors
* @emits ConsulKvMonitor#error
* @private
*/
_emitFactoryErrors(errors) {
setImmediate(() => {
errors.forEach(error => this.emit('error', error));
});
}
/**
* @private
*/
_setFallbackToWatchHealthy() {
if (this._fallbackToWatchHealthyInterval) {
this._unsetFallbackToWatchHealthy();
}
const initialUpdateTime = this._watchKvChange.updateTime();
this._fallbackToWatchHealthyInterval = setInterval(() => {
if (this.isWatchHealthy()) {
// watcher is currently ends or becomes `healthy`, unset fallback interval',
this._unsetFallbackToWatchHealthy();
return;
}
const lastUpdateTime = this._watchKvChange.updateTime();
if (initialUpdateTime !== lastUpdateTime) {
this._unsetFallbackToWatchHealthy();
this._setWatchHealthy();
}
}, HEALTH_FALLBACK_INTERVAL_MSEC);
}
/**
* @private
*/
_unsetFallbackToWatchHealthy() {
clearInterval(this._fallbackToWatchHealthyInterval);
this._fallbackToWatchHealthyInterval = null;
}
async _retryStartService() {
try {
const consulKvData = await this.start();
this._consulKvData = consulKvData;
this.emit('healthy');
this.emit('changed', consulKvData);
} catch (err) {
setImmediate(() => this.emit('error', err));
this._retryTimer = setTimeout(this._retryStartService, DEFAULT_RETRY_START_SERVICE_TIMEOUT_MSEC);
}
}
}
module.exports = ConsulKvMonitor;