@imqueue/core 2.0.3 → 2.0.5
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 +1 -1
- package/src/ClusterManager.d.ts +9 -3
- package/src/ClusterManager.js +16 -1
- package/src/ClusteredRedisQueue.d.ts +3 -2
- package/src/ClusteredRedisQueue.js +54 -11
- package/src/RedisQueue.d.ts +4 -4
- package/src/RedisQueue.js +13 -9
- package/src/UDPClusterManager.d.ts +15 -10
- package/src/UDPClusterManager.js +88 -27
- package/src/profile.js +1 -1
package/package.json
CHANGED
package/src/ClusterManager.d.ts
CHANGED
|
@@ -25,10 +25,16 @@ import { IMessageQueueConnection, IServerInput } from './IMessageQueue';
|
|
|
25
25
|
export interface ICluster {
|
|
26
26
|
add: (server: IServerInput) => void;
|
|
27
27
|
remove: (server: IServerInput) => void;
|
|
28
|
-
find: <T extends IMessageQueueConnection>(server: IServerInput) => T | undefined;
|
|
28
|
+
find: <T extends IMessageQueueConnection>(server: IServerInput, strict?: boolean) => T | undefined;
|
|
29
|
+
}
|
|
30
|
+
export interface InitializedCluster extends ICluster {
|
|
31
|
+
id: string;
|
|
29
32
|
}
|
|
30
33
|
export declare abstract class ClusterManager {
|
|
31
|
-
protected clusters:
|
|
34
|
+
protected clusters: InitializedCluster[];
|
|
32
35
|
protected constructor();
|
|
33
|
-
init(cluster: ICluster):
|
|
36
|
+
init(cluster: ICluster): InitializedCluster;
|
|
37
|
+
anyCluster(fn: (cluster: InitializedCluster) => Promise<void> | void): Promise<void>;
|
|
38
|
+
remove(cluster: string | InitializedCluster, destroy?: boolean): Promise<void>;
|
|
39
|
+
abstract destroy(): Promise<void>;
|
|
34
40
|
}
|
package/src/ClusterManager.js
CHANGED
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ClusterManager = void 0;
|
|
4
|
+
const uuid_1 = require("./uuid");
|
|
4
5
|
class ClusterManager {
|
|
5
6
|
constructor() {
|
|
6
7
|
this.clusters = [];
|
|
7
8
|
}
|
|
8
9
|
init(cluster) {
|
|
9
|
-
|
|
10
|
+
const initializedCluster = Object.assign(cluster, { id: (0, uuid_1.uuid)() });
|
|
11
|
+
this.clusters.push(initializedCluster);
|
|
12
|
+
return initializedCluster;
|
|
13
|
+
}
|
|
14
|
+
async anyCluster(fn) {
|
|
15
|
+
await Promise.all(this.clusters.map(cluster => fn(cluster)));
|
|
16
|
+
}
|
|
17
|
+
async remove(cluster, destroy = true) {
|
|
18
|
+
const id = typeof cluster === 'string' ? cluster : cluster.id;
|
|
19
|
+
this.clusters = this.clusters.filter(cluster => cluster.id !== id);
|
|
20
|
+
if (this.clusters.length === 0
|
|
21
|
+
&& destroy
|
|
22
|
+
&& typeof this.destroy === 'function') {
|
|
23
|
+
await this.destroy();
|
|
24
|
+
}
|
|
10
25
|
}
|
|
11
26
|
}
|
|
12
27
|
exports.ClusterManager = ClusterManager;
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* <support@imqueue.com> to get commercial licensing options.
|
|
23
23
|
*/
|
|
24
24
|
import { EventEmitter } from 'events';
|
|
25
|
-
import { ILogger, IMessageQueue, IMQMode, IMQOptions,
|
|
25
|
+
import { EventMap, ILogger, IMessageQueue, IMQMode, IMQOptions, IServerInput, JsonObject } from '.';
|
|
26
26
|
/**
|
|
27
27
|
* Class ClusteredRedisQueue
|
|
28
28
|
* Implements the possibility to scale queues horizontally between several
|
|
@@ -88,6 +88,7 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
88
88
|
*/
|
|
89
89
|
private readonly clusterEmitter;
|
|
90
90
|
private state;
|
|
91
|
+
private initializedClusters;
|
|
91
92
|
/**
|
|
92
93
|
* Class constructor
|
|
93
94
|
*
|
|
@@ -125,7 +126,7 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
125
126
|
*/
|
|
126
127
|
send(toQueue: string, message: JsonObject, delay?: number, errorHandler?: (err: Error) => void): Promise<string>;
|
|
127
128
|
/**
|
|
128
|
-
* Safely destroys current queue, unregistered all set event
|
|
129
|
+
* Safely destroys the current queue, unregistered all set event
|
|
129
130
|
* listeners and connections.
|
|
130
131
|
* Supposed to be an async function.
|
|
131
132
|
*
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
3
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
4
|
+
var m = o[Symbol.asyncIterator], i;
|
|
5
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
6
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
7
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
8
|
+
};
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.ClusteredRedisQueue = void 0;
|
|
4
11
|
/*!
|
|
@@ -73,6 +80,7 @@ class ClusteredRedisQueue {
|
|
|
73
80
|
started: false,
|
|
74
81
|
subscription: null,
|
|
75
82
|
};
|
|
83
|
+
this.initializedClusters = [];
|
|
76
84
|
this.templateEmitter = new events_1.EventEmitter();
|
|
77
85
|
this.clusterEmitter = new events_1.EventEmitter();
|
|
78
86
|
this.options = (0, _1.buildOptions)(_1.DEFAULT_IMQ_OPTIONS, options);
|
|
@@ -90,11 +98,11 @@ class ClusteredRedisQueue {
|
|
|
90
98
|
}
|
|
91
99
|
if ((_b = this.options.clusterManagers) === null || _b === void 0 ? void 0 : _b.length) {
|
|
92
100
|
for (const manager of this.options.clusterManagers) {
|
|
93
|
-
manager.init({
|
|
101
|
+
this.initializedClusters.push(manager.init({
|
|
94
102
|
add: this.addServer.bind(this),
|
|
95
103
|
remove: this.removeServer.bind(this),
|
|
96
104
|
find: this.findServer.bind(this),
|
|
97
|
-
});
|
|
105
|
+
}));
|
|
98
106
|
}
|
|
99
107
|
}
|
|
100
108
|
}
|
|
@@ -145,15 +153,49 @@ class ClusteredRedisQueue {
|
|
|
145
153
|
return id;
|
|
146
154
|
}
|
|
147
155
|
/**
|
|
148
|
-
* Safely destroys current queue, unregistered all set event
|
|
156
|
+
* Safely destroys the current queue, unregistered all set event
|
|
149
157
|
* listeners and connections.
|
|
150
158
|
* Supposed to be an async function.
|
|
151
159
|
*
|
|
152
160
|
* @returns {Promise<void>}
|
|
153
161
|
*/
|
|
154
162
|
async destroy() {
|
|
163
|
+
var _a, e_1, _b, _c, _d, e_2, _e, _f;
|
|
164
|
+
var _g;
|
|
155
165
|
this.state.started = false;
|
|
156
166
|
await this.batch('destroy', 'Destroying clustered redis message queue...');
|
|
167
|
+
if (!((_g = this.options.clusterManagers) === null || _g === void 0 ? void 0 : _g.length)) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
for (var _h = true, _j = __asyncValues(this.options.clusterManagers), _k; _k = await _j.next(), _a = _k.done, !_a; _h = true) {
|
|
172
|
+
_c = _k.value;
|
|
173
|
+
_h = false;
|
|
174
|
+
const manager = _c;
|
|
175
|
+
try {
|
|
176
|
+
for (var _l = true, _m = (e_2 = void 0, __asyncValues(this.initializedClusters)), _o; _o = await _m.next(), _d = _o.done, !_d; _l = true) {
|
|
177
|
+
_f = _o.value;
|
|
178
|
+
_l = false;
|
|
179
|
+
const cluster = _f;
|
|
180
|
+
await manager.remove(cluster);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
184
|
+
finally {
|
|
185
|
+
try {
|
|
186
|
+
if (!_l && !_d && (_e = _m.return)) await _e.call(_m);
|
|
187
|
+
}
|
|
188
|
+
finally { if (e_2) throw e_2.error; }
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
193
|
+
finally {
|
|
194
|
+
try {
|
|
195
|
+
if (!_h && !_a && (_b = _j.return)) await _b.call(_j);
|
|
196
|
+
}
|
|
197
|
+
finally { if (e_1) throw e_1.error; }
|
|
198
|
+
}
|
|
157
199
|
}
|
|
158
200
|
// noinspection JSUnusedGlobalSymbols
|
|
159
201
|
/**
|
|
@@ -325,7 +367,7 @@ class ClusteredRedisQueue {
|
|
|
325
367
|
* @returns {void}
|
|
326
368
|
*/
|
|
327
369
|
removeServer(server) {
|
|
328
|
-
const remove = this.findServer(server);
|
|
370
|
+
const remove = this.findServer(server, true);
|
|
329
371
|
if (!remove) {
|
|
330
372
|
return;
|
|
331
373
|
}
|
|
@@ -349,14 +391,12 @@ class ClusteredRedisQueue {
|
|
|
349
391
|
const opts = Object.assign(Object.assign({}, this.mqOptions), newServer);
|
|
350
392
|
const imq = new _1.RedisQueue(this.name, opts);
|
|
351
393
|
if (initializeQueue) {
|
|
352
|
-
this.initializeQueue(imq)
|
|
353
|
-
.then(() => {
|
|
394
|
+
this.initializeQueue(imq).then(() => {
|
|
354
395
|
this.clusterEmitter.emit('initialized', {
|
|
355
396
|
server: newServer,
|
|
356
397
|
imq,
|
|
357
398
|
});
|
|
358
|
-
})
|
|
359
|
-
.catch();
|
|
399
|
+
});
|
|
360
400
|
}
|
|
361
401
|
newServer.imq = imq;
|
|
362
402
|
this.imqs.push(imq);
|
|
@@ -376,16 +416,19 @@ class ClusteredRedisQueue {
|
|
|
376
416
|
await imq.subscribe(this.state.subscription.channel, this.state.subscription.handler);
|
|
377
417
|
}
|
|
378
418
|
}
|
|
379
|
-
findServer(server) {
|
|
380
|
-
return this.servers.find(existing => ClusteredRedisQueue.matchServers(existing, server));
|
|
419
|
+
findServer(server, strict = false) {
|
|
420
|
+
return this.servers.find(existing => ClusteredRedisQueue.matchServers(existing, server, strict));
|
|
381
421
|
}
|
|
382
|
-
static matchServers(source, target) {
|
|
422
|
+
static matchServers(source, target, strict = false) {
|
|
383
423
|
const sameAddress = target.host === source.host
|
|
384
424
|
&& target.port === source.port;
|
|
385
425
|
if (!target.id && !source.id) {
|
|
386
426
|
return sameAddress;
|
|
387
427
|
}
|
|
388
428
|
const sameId = target.id === source.id;
|
|
429
|
+
if (strict) {
|
|
430
|
+
return sameId && sameAddress;
|
|
431
|
+
}
|
|
389
432
|
return sameId || sameAddress;
|
|
390
433
|
}
|
|
391
434
|
}
|
package/src/RedisQueue.d.ts
CHANGED
|
@@ -92,7 +92,7 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
|
|
|
92
92
|
*/
|
|
93
93
|
private destroyed;
|
|
94
94
|
/**
|
|
95
|
-
* True if the current instance owns watcher connection, false otherwise
|
|
95
|
+
* True if the current instance owns a watcher connection, false otherwise
|
|
96
96
|
*
|
|
97
97
|
* @type {boolean}
|
|
98
98
|
*/
|
|
@@ -230,7 +230,7 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
|
|
|
230
230
|
*/
|
|
231
231
|
private get watcher();
|
|
232
232
|
/**
|
|
233
|
-
* Watcher setter
|
|
233
|
+
* Watcher setter sets the watcher connection property for this
|
|
234
234
|
* queue instance
|
|
235
235
|
*
|
|
236
236
|
* @param {IRedisClient} conn
|
|
@@ -332,14 +332,14 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
|
|
|
332
332
|
*/
|
|
333
333
|
private process;
|
|
334
334
|
/**
|
|
335
|
-
* Returns number of established watcher connections on redis
|
|
335
|
+
* Returns the number of established watcher connections on redis
|
|
336
336
|
*
|
|
337
337
|
* @access private
|
|
338
338
|
* @returns {Promise<number>}
|
|
339
339
|
*/
|
|
340
340
|
private watcherCount;
|
|
341
341
|
/**
|
|
342
|
-
* Processes delayed message by its given redis key
|
|
342
|
+
* Processes delayed a message by its given redis key
|
|
343
343
|
*
|
|
344
344
|
* @access private
|
|
345
345
|
* @param {string} key
|
package/src/RedisQueue.js
CHANGED
|
@@ -128,7 +128,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
128
128
|
*/
|
|
129
129
|
this.destroyed = false;
|
|
130
130
|
/**
|
|
131
|
-
* True if the current instance owns watcher connection, false otherwise
|
|
131
|
+
* True if the current instance owns a watcher connection, false otherwise
|
|
132
132
|
*
|
|
133
133
|
* @type {boolean}
|
|
134
134
|
*/
|
|
@@ -246,6 +246,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
246
246
|
if (this.initialized) {
|
|
247
247
|
return this;
|
|
248
248
|
}
|
|
249
|
+
this.destroyed = false;
|
|
249
250
|
const connPromises = [];
|
|
250
251
|
// istanbul ignore next
|
|
251
252
|
if (!this.reader && this.isWorker()) {
|
|
@@ -277,7 +278,6 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
277
278
|
}
|
|
278
279
|
await this.initWatcher();
|
|
279
280
|
this.initialized = true;
|
|
280
|
-
this.destroyed = false;
|
|
281
281
|
return this;
|
|
282
282
|
}
|
|
283
283
|
/**
|
|
@@ -297,6 +297,9 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
297
297
|
if (!this.writer) {
|
|
298
298
|
await this.start();
|
|
299
299
|
}
|
|
300
|
+
if (!this.writer) {
|
|
301
|
+
throw new TypeError('IMQ: unable to initialize queue!');
|
|
302
|
+
}
|
|
300
303
|
const id = (0, _1.uuid)();
|
|
301
304
|
const data = { id, message, from: this.name };
|
|
302
305
|
const key = `${this.options.prefix}:${toQueue}`;
|
|
@@ -350,6 +353,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
350
353
|
await this.stop();
|
|
351
354
|
await this.clear();
|
|
352
355
|
this.destroyWriter();
|
|
356
|
+
await this.unsubscribe();
|
|
353
357
|
}
|
|
354
358
|
/**
|
|
355
359
|
* Clears queue data in redis;
|
|
@@ -419,7 +423,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
419
423
|
}
|
|
420
424
|
// noinspection JSUnusedLocalSymbols
|
|
421
425
|
/**
|
|
422
|
-
* Watcher setter
|
|
426
|
+
* Watcher setter sets the watcher connection property for this
|
|
423
427
|
* queue instance
|
|
424
428
|
*
|
|
425
429
|
* @param {IRedisClient} conn
|
|
@@ -632,22 +636,22 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
632
636
|
return this;
|
|
633
637
|
}
|
|
634
638
|
/**
|
|
635
|
-
* Returns number of established watcher connections on redis
|
|
639
|
+
* Returns the number of established watcher connections on redis
|
|
636
640
|
*
|
|
637
641
|
* @access private
|
|
638
642
|
* @returns {Promise<number>}
|
|
639
643
|
*/
|
|
640
644
|
// istanbul ignore next
|
|
641
645
|
async watcherCount() {
|
|
646
|
+
if (!this.writer) {
|
|
647
|
+
return 0;
|
|
648
|
+
}
|
|
642
649
|
const rx = new RegExp(`\\bname=${this.options.prefix}:[\\S]+?:watcher:`);
|
|
643
650
|
const list = await this.writer.client('LIST');
|
|
644
|
-
return (
|
|
645
|
-
.split(/\r?\n/)
|
|
646
|
-
.filter(client => rx.test(client))
|
|
647
|
-
.length;
|
|
651
|
+
return list.split(/\r?\n/).filter(client => rx.test(client)).length;
|
|
648
652
|
}
|
|
649
653
|
/**
|
|
650
|
-
* Processes delayed message by its given redis key
|
|
654
|
+
* Processes delayed a message by its given redis key
|
|
651
655
|
*
|
|
652
656
|
* @access private
|
|
653
657
|
* @param {string} key
|
|
@@ -1,18 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const
|
|
1
|
+
import { ClusterManager } from './ClusterManager';
|
|
2
|
+
export declare const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS: {
|
|
3
3
|
broadcastPort: number;
|
|
4
4
|
broadcastAddress: string;
|
|
5
5
|
aliveTimeoutCorrection: number;
|
|
6
6
|
};
|
|
7
7
|
export interface UDPClusterManagerOptions {
|
|
8
|
-
/**
|
|
9
|
-
* Represents the cluster operations that are responsible for managing
|
|
10
|
-
* clusters. This includes operations such as adding, removing, or checking
|
|
11
|
-
* if a cluster server exists.
|
|
12
|
-
*
|
|
13
|
-
* @type {ICluster}
|
|
14
|
-
*/
|
|
15
|
-
cluster?: ICluster;
|
|
16
8
|
/**
|
|
17
9
|
* Message queue broadcast port
|
|
18
10
|
*
|
|
@@ -70,7 +62,11 @@ export interface UDPClusterManagerOptions {
|
|
|
70
62
|
export declare class UDPClusterManager extends ClusterManager {
|
|
71
63
|
private static sockets;
|
|
72
64
|
private readonly options;
|
|
65
|
+
private socketKey;
|
|
66
|
+
private get socket();
|
|
67
|
+
private set socket(value);
|
|
73
68
|
constructor(options?: UDPClusterManagerOptions);
|
|
69
|
+
private static free;
|
|
74
70
|
private listenBroadcastedMessages;
|
|
75
71
|
private startListening;
|
|
76
72
|
private static verifyHosts;
|
|
@@ -78,5 +74,14 @@ export declare class UDPClusterManager extends ClusterManager {
|
|
|
78
74
|
private static processBroadcastedMessage;
|
|
79
75
|
private static parseBroadcastedMessage;
|
|
80
76
|
private static serverAliveWait;
|
|
77
|
+
/**
|
|
78
|
+
* Destroys the UDPClusterManager by closing all opened network connections
|
|
79
|
+
* and safely destroying all blocking sockets
|
|
80
|
+
*
|
|
81
|
+
* @returns {Promise<void>}
|
|
82
|
+
* @throws {Error}
|
|
83
|
+
*/
|
|
84
|
+
destroy(): Promise<void>;
|
|
85
|
+
private static destroySocket;
|
|
81
86
|
private static selectNetworkInterface;
|
|
82
87
|
}
|
package/src/UDPClusterManager.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UDPClusterManager = exports.
|
|
3
|
+
exports.UDPClusterManager = exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = void 0;
|
|
4
4
|
const ClusterManager_1 = require("./ClusterManager");
|
|
5
5
|
const dgram_1 = require("dgram");
|
|
6
6
|
const os_1 = require("os");
|
|
7
|
-
var
|
|
8
|
-
(function (
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
})(
|
|
12
|
-
exports.
|
|
7
|
+
var MessageType;
|
|
8
|
+
(function (MessageType) {
|
|
9
|
+
MessageType["Up"] = "up";
|
|
10
|
+
MessageType["Down"] = "down";
|
|
11
|
+
})(MessageType || (MessageType = {}));
|
|
12
|
+
exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = {
|
|
13
13
|
broadcastPort: 63000,
|
|
14
14
|
broadcastAddress: '255.255.255.255',
|
|
15
15
|
aliveTimeoutCorrection: 1000,
|
|
@@ -30,20 +30,32 @@ const LOCALHOST_ADDRESSES = [
|
|
|
30
30
|
* ~~~
|
|
31
31
|
*/
|
|
32
32
|
class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
33
|
+
get socket() {
|
|
34
|
+
return UDPClusterManager.sockets[this.socketKey];
|
|
35
|
+
}
|
|
36
|
+
set socket(socket) {
|
|
37
|
+
UDPClusterManager.sockets[this.socketKey] = socket;
|
|
38
|
+
}
|
|
33
39
|
constructor(options) {
|
|
34
40
|
super();
|
|
35
|
-
this.options = Object.assign(Object.assign({}, exports.
|
|
41
|
+
this.options = Object.assign(Object.assign({}, exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS), options || {});
|
|
36
42
|
this.startListening(this.options);
|
|
43
|
+
process.on('SIGTERM', UDPClusterManager.free);
|
|
44
|
+
process.on('SIGINT', UDPClusterManager.free);
|
|
45
|
+
process.on('SIGABRT', UDPClusterManager.free);
|
|
46
|
+
}
|
|
47
|
+
static async free() {
|
|
48
|
+
const socketKeys = Object.keys(UDPClusterManager.sockets);
|
|
49
|
+
await Promise.all(socketKeys.map(socketKey => UDPClusterManager.destroySocket(socketKey, UDPClusterManager.sockets[socketKey])));
|
|
37
50
|
}
|
|
38
51
|
listenBroadcastedMessages(listener, options) {
|
|
39
52
|
const address = UDPClusterManager.selectNetworkInterface(options);
|
|
40
|
-
|
|
41
|
-
if (!
|
|
42
|
-
|
|
43
|
-
socket.bind(options.broadcastPort, address);
|
|
44
|
-
UDPClusterManager.sockets[key] = socket;
|
|
53
|
+
this.socketKey = `${address}:${options.broadcastPort}`;
|
|
54
|
+
if (!this.socket) {
|
|
55
|
+
this.socket = (0, dgram_1.createSocket)({ type: 'udp4', reuseAddr: true });
|
|
56
|
+
this.socket.bind(options.broadcastPort, address);
|
|
45
57
|
}
|
|
46
|
-
|
|
58
|
+
this.socket.on('message', message => listener(UDPClusterManager.parseBroadcastedMessage(message)));
|
|
47
59
|
}
|
|
48
60
|
startListening(options = {}) {
|
|
49
61
|
this.listenBroadcastedMessages(UDPClusterManager.processBroadcastedMessage(this), options);
|
|
@@ -56,19 +68,19 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
56
68
|
}
|
|
57
69
|
static processMessageOnCluster(cluster, message, aliveTimeoutCorrection) {
|
|
58
70
|
const server = cluster.find(message);
|
|
59
|
-
if (server && message.type ===
|
|
71
|
+
if (server && message.type === MessageType.Down) {
|
|
60
72
|
clearTimeout(server.timer);
|
|
61
73
|
return cluster.remove(message);
|
|
62
74
|
}
|
|
63
|
-
if (!server && message.type ===
|
|
75
|
+
if (!server && message.type === MessageType.Up) {
|
|
64
76
|
cluster.add(message);
|
|
65
|
-
const added = cluster.find(message);
|
|
77
|
+
const added = cluster.find(message, true);
|
|
66
78
|
if (added) {
|
|
67
79
|
UDPClusterManager.serverAliveWait(cluster, added, aliveTimeoutCorrection);
|
|
68
80
|
}
|
|
69
81
|
return;
|
|
70
82
|
}
|
|
71
|
-
if (server && message.type ===
|
|
83
|
+
if (server && message.type === MessageType.Up) {
|
|
72
84
|
return UDPClusterManager.serverAliveWait(cluster, server, aliveTimeoutCorrection, message);
|
|
73
85
|
}
|
|
74
86
|
}
|
|
@@ -82,9 +94,9 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
82
94
|
&& !UDPClusterManager.verifyHosts(message.host, context.options.includeHosts)) {
|
|
83
95
|
return;
|
|
84
96
|
}
|
|
85
|
-
|
|
97
|
+
context.anyCluster(cluster => {
|
|
86
98
|
UDPClusterManager.processMessageOnCluster(cluster, message, context.options.aliveTimeoutCorrection);
|
|
87
|
-
}
|
|
99
|
+
}).then();
|
|
88
100
|
};
|
|
89
101
|
}
|
|
90
102
|
static parseBroadcastedMessage(input) {
|
|
@@ -100,26 +112,75 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
100
112
|
};
|
|
101
113
|
}
|
|
102
114
|
static serverAliveWait(cluster, server, aliveTimeoutCorrection, message) {
|
|
103
|
-
|
|
115
|
+
if (server.timer) {
|
|
116
|
+
clearTimeout(server.timer);
|
|
117
|
+
server.timer = undefined;
|
|
118
|
+
}
|
|
104
119
|
server.timestamp = Date.now();
|
|
105
120
|
if (message) {
|
|
106
121
|
server.timeout = message.timeout;
|
|
107
122
|
}
|
|
108
123
|
const correction = aliveTimeoutCorrection || 0;
|
|
109
124
|
const timeout = (server.timeout || 0) + correction;
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
125
|
+
if (timeout <= 0) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const timerId = setTimeout(() => {
|
|
129
|
+
const existing = cluster.find(server, true);
|
|
130
|
+
if (!existing || existing.timer !== timerId) {
|
|
113
131
|
return;
|
|
114
132
|
}
|
|
115
133
|
const now = Date.now();
|
|
116
|
-
|
|
134
|
+
if (!existing.timestamp) {
|
|
135
|
+
clearTimeout(existing.timer);
|
|
136
|
+
existing.timer = undefined;
|
|
137
|
+
cluster.remove(existing);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const delta = now - existing.timestamp;
|
|
117
141
|
const currentTimeout = (existing.timeout || 0) + correction;
|
|
118
142
|
if (delta >= currentTimeout) {
|
|
119
|
-
clearTimeout(
|
|
120
|
-
|
|
143
|
+
clearTimeout(existing.timer);
|
|
144
|
+
existing.timer = undefined;
|
|
145
|
+
cluster.remove(existing);
|
|
121
146
|
}
|
|
122
147
|
}, timeout);
|
|
148
|
+
server.timer = timerId;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Destroys the UDPClusterManager by closing all opened network connections
|
|
152
|
+
* and safely destroying all blocking sockets
|
|
153
|
+
*
|
|
154
|
+
* @returns {Promise<void>}
|
|
155
|
+
* @throws {Error}
|
|
156
|
+
*/
|
|
157
|
+
async destroy() {
|
|
158
|
+
await UDPClusterManager.destroySocket(this.socketKey, this.socket);
|
|
159
|
+
}
|
|
160
|
+
static async destroySocket(socketKey, socket) {
|
|
161
|
+
if (!socket) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
return await new Promise((resolve, reject) => {
|
|
165
|
+
try {
|
|
166
|
+
if (typeof socket.close === 'function') {
|
|
167
|
+
socket.removeAllListeners();
|
|
168
|
+
socket.close(() => {
|
|
169
|
+
socket === null || socket === void 0 ? void 0 : socket.unref();
|
|
170
|
+
if (socketKey
|
|
171
|
+
&& UDPClusterManager.sockets[socketKey]) {
|
|
172
|
+
delete UDPClusterManager.sockets[socketKey];
|
|
173
|
+
}
|
|
174
|
+
resolve();
|
|
175
|
+
});
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
resolve();
|
|
179
|
+
}
|
|
180
|
+
catch (e) {
|
|
181
|
+
reject(e);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
123
184
|
}
|
|
124
185
|
static selectNetworkInterface(options) {
|
|
125
186
|
const interfaces = (0, os_1.networkInterfaces)();
|
package/src/profile.js
CHANGED
|
@@ -98,7 +98,7 @@ function logDebugInfo({ debugTime, debugArgs, className, args, methodName, start
|
|
|
98
98
|
? logger[logLevel].bind(logger) : undefined;
|
|
99
99
|
if (debugTime) {
|
|
100
100
|
// noinspection TypeScriptUnresolvedFunction
|
|
101
|
-
const time = parseInt((process.hrtime.bigint() - start), 10) / 1000;
|
|
101
|
+
const time = parseInt((process.hrtime.bigint() - BigInt(start)), 10) / 1000;
|
|
102
102
|
let timeStr;
|
|
103
103
|
// istanbul ignore next
|
|
104
104
|
switch (exports.IMQ_LOG_TIME_FORMAT) {
|