@imqueue/core 2.0.25 → 2.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imqueue/core",
3
- "version": "2.0.25",
3
+ "version": "2.0.26",
4
4
  "description": "Simple JSON-based messaging queue for inter service communication",
5
5
  "keywords": [
6
6
  "message-queue",
@@ -144,6 +144,7 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
144
144
  */
145
145
  clear(): Promise<ClusteredRedisQueue>;
146
146
  queueLength(): Promise<number>;
147
+ private verbose;
147
148
  /**
148
149
  * Batch imq action processing on all registered imqs at once
149
150
  *
@@ -97,6 +97,7 @@ class ClusteredRedisQueue {
97
97
  this.addServerWithQueueInitializing(server, false);
98
98
  }
99
99
  if ((_b = this.options.clusterManagers) === null || _b === void 0 ? void 0 : _b.length) {
100
+ this.verbose('Initializing cluster managers...');
100
101
  for (const manager of this.options.clusterManagers) {
101
102
  this.initializedClusters.push(manager.init({
102
103
  add: this.addServer.bind(this),
@@ -215,6 +216,11 @@ class ClusteredRedisQueue {
215
216
  const lengths = await Promise.all(promises);
216
217
  return lengths.reduce((total, length) => total + length, 0);
217
218
  }
219
+ verbose(message) {
220
+ if (this.options.verbose) {
221
+ this.logger.info(`[IMQ-CORE][ClusteredRedisQueue][${this.name}]: ${message}`);
222
+ }
223
+ }
218
224
  /**
219
225
  * Batch imq action processing on all registered imqs at once
220
226
  *
@@ -224,7 +230,7 @@ class ClusteredRedisQueue {
224
230
  * @return {Promise<this>}
225
231
  */
226
232
  async batch(action, message) {
227
- this.logger.log(message);
233
+ this.logger.info(message);
228
234
  const promises = [];
229
235
  for (const imq of this.imqs) {
230
236
  promises.push(imq[action]());
@@ -366,6 +372,7 @@ class ClusteredRedisQueue {
366
372
  * @returns {void}
367
373
  */
368
374
  addServer(server) {
375
+ this.verbose(`Adding new server: ${JSON.stringify(server)}`);
369
376
  return this.addServerWithQueueInitializing(server, true);
370
377
  }
371
378
  /**
@@ -375,6 +382,7 @@ class ClusteredRedisQueue {
375
382
  * @returns {void}
376
383
  */
377
384
  removeServer(server) {
385
+ this.verbose(`Removing the server: ${JSON.stringify(server)}`);
378
386
  const remove = this.findServer(server);
379
387
  if (!remove) {
380
388
  return;
@@ -423,6 +431,7 @@ class ClusteredRedisQueue {
423
431
  }
424
432
  async initializeQueue(imq) {
425
433
  (0, _1.copyEventEmitter)(this.templateEmitter, imq);
434
+ this.verbose(`Initializing queue with state: ${JSON.stringify(this.state)}`);
426
435
  if (this.state.started) {
427
436
  await imq.start();
428
437
  }
package/src/RedisQueue.js CHANGED
@@ -662,12 +662,10 @@ class RedisQueue extends events_1.EventEmitter {
662
662
  if (this.reconnecting[channel]) {
663
663
  return;
664
664
  }
665
- this.reconnecting[channel] = true;
666
665
  const attempts = (this.reconnectAttempts[channel] || 0) + 1;
666
+ const delay = Math.min(30000, 1000 * Math.pow(2, attempts - 1));
667
+ this.reconnecting[channel] = true;
667
668
  this.reconnectAttempts[channel] = attempts;
668
- const base = Math.min(30000, 1000 * Math.pow(2, attempts - 1));
669
- const jitter = Math.floor(base * 0.2 * Math.random());
670
- const delay = base + jitter;
671
669
  this.verbose(`Scheduling ${channel} reconnect in ${delay} ms (attempt ${attempts})`);
672
670
  if (this.reconnectTimers[channel]) {
673
671
  clearTimeout(this.reconnectTimers[channel]);
@@ -51,6 +51,16 @@ export interface UDPClusterManagerOptions {
51
51
  * @type {number}
52
52
  */
53
53
  aliveTimeoutCorrection: number;
54
+ /**
55
+ * Message queue alive-server check flag. If set to false, the server will
56
+ * not be checked for liveness on each broadcast message with a timeout.
57
+ * Can be specified by the environment variable if the given option is not
58
+ * bypassed: IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK
59
+ *
60
+ * @default true
61
+ * @type {boolean}
62
+ */
63
+ useAliveCheck: boolean;
54
64
  }
55
65
  export declare const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS: UDPClusterManagerOptions;
56
66
  export declare class UDPClusterManager extends ClusterManager {
@@ -28,10 +28,12 @@ const ClusterManager_1 = require("./ClusterManager");
28
28
  const worker_threads_1 = require("worker_threads");
29
29
  const path = require("path");
30
30
  process.setMaxListeners(10000);
31
+ const IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK = !!+(process.env.IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK || 1);
31
32
  exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = {
32
33
  port: 63000,
33
34
  address: '255.255.255.255',
34
35
  aliveTimeoutCorrection: 5000,
36
+ useAliveCheck: IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK,
35
37
  };
36
38
  class UDPClusterManager extends ClusterManager_1.ClusterManager {
37
39
  constructor(options) {
package/src/UDPWorker.js CHANGED
@@ -34,7 +34,7 @@ var MessageType;
34
34
  MessageType["Up"] = "up";
35
35
  MessageType["Down"] = "down";
36
36
  })(MessageType || (MessageType = {}));
37
- class UDPClusterWorker {
37
+ class UDPWorker {
38
38
  constructor(options, messagePort) {
39
39
  this.options = options;
40
40
  this.messagePort = messagePort;
@@ -66,15 +66,17 @@ class UDPClusterWorker {
66
66
  addServer(message) {
67
67
  this.messagePort.postMessage({
68
68
  type: 'cluster:add',
69
- server: UDPClusterWorker.mapMessage(message),
69
+ server: UDPWorker.mapMessage(message),
70
70
  });
71
- this.serverAliveWait(message);
71
+ if (this.options.useAliveCheck) {
72
+ this.serverAliveWait(message);
73
+ }
72
74
  }
73
75
  removeServer(message) {
74
- this.servers.delete(UDPClusterWorker.getServerKey(message));
76
+ this.servers.delete(UDPWorker.getServerKey(message));
75
77
  this.messagePort.postMessage({
76
78
  type: 'cluster:remove',
77
- server: UDPClusterWorker.mapMessage(message),
79
+ server: UDPWorker.mapMessage(message),
78
80
  });
79
81
  }
80
82
  static mapMessage(message) {
@@ -92,7 +94,7 @@ class UDPClusterWorker {
92
94
  const stamp = (0, uuid_1.uuid)();
93
95
  const correction = (_a = this.options.aliveTimeoutCorrection) !== null && _a !== void 0 ? _a : 0;
94
96
  const effectiveTimeout = message.timeout + correction + 1;
95
- const key = UDPClusterWorker.getServerKey(message);
97
+ const key = UDPWorker.getServerKey(message);
96
98
  this.servers.set(key, stamp);
97
99
  const t = setTimeout(() => setImmediate(() => {
98
100
  if (this.servers.get(key) === stamp) {
@@ -168,6 +170,6 @@ class UDPClusterWorker {
168
170
  }
169
171
  }
170
172
  if (!worker_threads_1.isMainThread && worker_threads_1.parentPort) {
171
- new UDPClusterWorker(worker_threads_1.workerData, worker_threads_1.parentPort);
173
+ new UDPWorker(worker_threads_1.workerData, worker_threads_1.parentPort);
172
174
  }
173
175
  //# sourceMappingURL=UDPWorker.js.map