@imqueue/core 2.0.9 → 2.0.11
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 +6 -4
- package/src/ClusteredRedisQueue.js +11 -6
- package/src/RedisQueue.d.ts +1 -1
- package/src/RedisQueue.js +1 -0
- package/src/UDPClusterManager.d.ts +36 -39
- package/src/UDPClusterManager.js +102 -142
- package/src/UDPWorker.d.ts +1 -0
- package/src/UDPWorker.js +173 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imqueue/core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.11",
|
|
4
4
|
"description": "Simple JSON-based messaging queue for inter service communication",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"message-queue",
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"scripts": {
|
|
14
14
|
"benchmark": "node benchmark -c $(( $(nproc) - 2 )) -m 100000",
|
|
15
15
|
"prepare": "./node_modules/.bin/tsc",
|
|
16
|
-
"test": "./node_modules/.bin/tsc && ./node_modules/.bin/nyc mocha && ./node_modules/.bin/nyc report --reporter=text-lcov",
|
|
17
|
-
"test-fast": "./node_modules/.bin/tsc && ./node_modules/.bin/nyc mocha && /usr/bin/env node -e \"import('open').then(open => open.default('file://`pwd`/coverage/index.html', { wait: false }))\"",
|
|
16
|
+
"test": "./node_modules/.bin/tsc && ./node_modules/.bin/nyc mocha --exit --timeout 10000 && ./node_modules/.bin/nyc report --reporter=text-lcov",
|
|
17
|
+
"test-fast": "./node_modules/.bin/tsc && ./node_modules/.bin/nyc mocha --exit --timeout 10000 && /usr/bin/env node -e \"import('open').then(open => open.default('file://`pwd`/coverage/index.html', { wait: false }))\"",
|
|
18
18
|
"test-local": "export COVERALLS_REPO_TOKEN=$IMQ_COVERALLS_TOKEN && npm test && /usr/bin/env node -e \"import('open').then(open => open.default('https://coveralls.io/github/imqueue/imq', { wait: false }))\"",
|
|
19
19
|
"test-dev": "npm run test && npm run clean-js && npm run clean-typedefs && npm run clean-maps",
|
|
20
20
|
"test-coverage": "cat ./coverage/lcov.info | CODECLIMATE_API_HOST=https://codebeat.co/webhooks/code_coverage CODECLIMATE_REPO_TOKEN=85bb2a18-4ebb-4e48-a2ce-92b7bf438b1a ./node_modules/.bin/codeclimate-test-reporter",
|
|
@@ -80,7 +80,9 @@
|
|
|
80
80
|
],
|
|
81
81
|
"recursive": true,
|
|
82
82
|
"bail": true,
|
|
83
|
-
"full-trace": true
|
|
83
|
+
"full-trace": true,
|
|
84
|
+
"exit": true,
|
|
85
|
+
"timeout": 10000
|
|
84
86
|
},
|
|
85
87
|
"nyc": {
|
|
86
88
|
"check-coverage": false,
|
|
@@ -371,18 +371,23 @@ class ClusteredRedisQueue {
|
|
|
371
371
|
if (!remove) {
|
|
372
372
|
return;
|
|
373
373
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
374
|
+
const imqToRemove = remove.imq;
|
|
375
|
+
if (imqToRemove) {
|
|
376
|
+
this.imqs = this.imqs.filter(imq => imqToRemove.redisKey !== imq.redisKey);
|
|
377
|
+
imqToRemove.destroy().catch();
|
|
377
378
|
}
|
|
379
|
+
this.queueLength = this.imqs.length;
|
|
380
|
+
this.servers = this.servers.filter(existing => !ClusteredRedisQueue.matchServers(existing, server));
|
|
378
381
|
this.clusterEmitter.emit('remove', {
|
|
379
382
|
server: remove,
|
|
380
|
-
imq:
|
|
383
|
+
imq: imqToRemove,
|
|
381
384
|
});
|
|
382
|
-
this.queueLength = this.imqs.length;
|
|
383
|
-
this.servers = this.servers.filter(existing => !ClusteredRedisQueue.matchServers(existing, server));
|
|
384
385
|
}
|
|
385
386
|
addServerWithQueueInitializing(server, initializeQueue = true) {
|
|
387
|
+
const existingServer = this.findServer(server);
|
|
388
|
+
if (existingServer) {
|
|
389
|
+
return existingServer;
|
|
390
|
+
}
|
|
386
391
|
const newServer = {
|
|
387
392
|
id: server.id,
|
|
388
393
|
host: server.host,
|
package/src/RedisQueue.d.ts
CHANGED
|
@@ -110,7 +110,7 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
|
|
|
110
110
|
/**
|
|
111
111
|
* This queue instance unique key (identifier), for internal use
|
|
112
112
|
*/
|
|
113
|
-
|
|
113
|
+
readonly redisKey: string;
|
|
114
114
|
/**
|
|
115
115
|
* LUA scripts for redis
|
|
116
116
|
*
|
package/src/RedisQueue.js
CHANGED
|
@@ -511,6 +511,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
511
511
|
connectionName: this.getChannelName(context.name, options.prefix || '', channel),
|
|
512
512
|
retryStrategy: this.retryStrategy(context),
|
|
513
513
|
autoResubscribe: true,
|
|
514
|
+
enableReadyCheck: true,
|
|
514
515
|
});
|
|
515
516
|
context[channel] = (0, _1.makeRedisSafe)(redis);
|
|
516
517
|
context[channel].__imq = true;
|
|
@@ -1,9 +1,27 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* UDP message listener for cluster managing
|
|
3
|
+
*
|
|
4
|
+
* I'm Queue Software Project
|
|
5
|
+
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
6
|
+
*
|
|
7
|
+
* This program is free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the GNU General Public License as published by
|
|
9
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
* (at your option) any later version.
|
|
11
|
+
*
|
|
12
|
+
* This program is distributed in the hope that it will be useful,
|
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
+
* GNU General Public License for more details.
|
|
16
|
+
*
|
|
17
|
+
* You should have received a copy of the GNU General Public License
|
|
18
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
|
+
*
|
|
20
|
+
* If you want to use this code in a closed source (commercial) project, you can
|
|
21
|
+
* purchase a proprietary commercial license. Please contact us at
|
|
22
|
+
* <support@imqueue.com> to get commercial licensing options.
|
|
23
|
+
*/
|
|
1
24
|
import { ClusterManager } from './ClusterManager';
|
|
2
|
-
export declare const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS: {
|
|
3
|
-
broadcastPort: number;
|
|
4
|
-
broadcastAddress: string;
|
|
5
|
-
aliveTimeoutCorrection: number;
|
|
6
|
-
};
|
|
7
25
|
export interface UDPClusterManagerOptions {
|
|
8
26
|
/**
|
|
9
27
|
* Message queue broadcast port
|
|
@@ -11,61 +29,40 @@ export interface UDPClusterManagerOptions {
|
|
|
11
29
|
* @default 63000
|
|
12
30
|
* @type {number}
|
|
13
31
|
*/
|
|
14
|
-
|
|
32
|
+
port: number;
|
|
15
33
|
/**
|
|
16
34
|
* Message queue broadcast address
|
|
17
35
|
*
|
|
18
|
-
* @default limitedBroadcastAddress
|
|
19
36
|
* @type {number}
|
|
20
37
|
*/
|
|
21
|
-
|
|
38
|
+
address: string;
|
|
22
39
|
/**
|
|
23
40
|
* Message queue limited broadcast address
|
|
24
41
|
*
|
|
25
|
-
* @default 255.255.255.255
|
|
42
|
+
* @default "255.255.255.255"
|
|
26
43
|
* @type {string}
|
|
27
44
|
*/
|
|
28
|
-
|
|
45
|
+
limitedAddress?: string;
|
|
29
46
|
/**
|
|
30
47
|
* Message queue alive timeout correction. Used to correct waiting time to
|
|
31
48
|
* check if the server is alive
|
|
32
49
|
*
|
|
33
|
-
* @default
|
|
50
|
+
* @default 5000
|
|
34
51
|
* @type {number}
|
|
35
52
|
*/
|
|
36
53
|
aliveTimeoutCorrection: number;
|
|
37
54
|
}
|
|
38
|
-
|
|
39
|
-
* UDP broadcast-based cluster management implementation
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ~~~typescript
|
|
43
|
-
* const queue = new ClusteredRedisQueue('ClusteredQueue', {
|
|
44
|
-
* clusterManagers: [new UDPBroadcastClusterManager()],
|
|
45
|
-
* });
|
|
46
|
-
* ~~~
|
|
47
|
-
*/
|
|
55
|
+
export declare const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS: UDPClusterManagerOptions;
|
|
48
56
|
export declare class UDPClusterManager extends ClusterManager {
|
|
49
|
-
private static
|
|
57
|
+
private static workers;
|
|
58
|
+
static sockets: Record<string, any>;
|
|
50
59
|
private readonly options;
|
|
51
|
-
private
|
|
52
|
-
private
|
|
53
|
-
private set socket(value);
|
|
60
|
+
private workerKey;
|
|
61
|
+
private worker;
|
|
54
62
|
constructor(options?: Partial<UDPClusterManagerOptions>);
|
|
55
63
|
private static free;
|
|
56
|
-
private
|
|
57
|
-
private startListening;
|
|
58
|
-
private static processMessageOnCluster;
|
|
59
|
-
private static parseBroadcastedMessage;
|
|
60
|
-
private static serverAliveWait;
|
|
61
|
-
/**
|
|
62
|
-
* Destroys the UDPClusterManager by closing all opened network connections
|
|
63
|
-
* and safely destroying all blocking sockets
|
|
64
|
-
*
|
|
65
|
-
* @returns {Promise<void>}
|
|
66
|
-
* @throws {Error}
|
|
67
|
-
*/
|
|
64
|
+
private startWorkerListener;
|
|
68
65
|
destroy(): Promise<void>;
|
|
69
|
-
|
|
70
|
-
private static
|
|
66
|
+
static destroySocket(key: string, socket?: any): Promise<void>;
|
|
67
|
+
private static destroyWorker;
|
|
71
68
|
}
|
package/src/UDPClusterManager.js
CHANGED
|
@@ -1,179 +1,139 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UDPClusterManager = exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = void 0;
|
|
4
|
+
/*!
|
|
5
|
+
* UDP message listener for cluster managing
|
|
6
|
+
*
|
|
7
|
+
* I'm Queue Software Project
|
|
8
|
+
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
9
|
+
*
|
|
10
|
+
* This program is free software: you can redistribute it and/or modify
|
|
11
|
+
* it under the terms of the GNU General Public License as published by
|
|
12
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
13
|
+
* (at your option) any later version.
|
|
14
|
+
*
|
|
15
|
+
* This program is distributed in the hope that it will be useful,
|
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18
|
+
* GNU General Public License for more details.
|
|
19
|
+
*
|
|
20
|
+
* You should have received a copy of the GNU General Public License
|
|
21
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
22
|
+
*
|
|
23
|
+
* If you want to use this code in a closed source (commercial) project, you can
|
|
24
|
+
* purchase a proprietary commercial license. Please contact us at
|
|
25
|
+
* <support@imqueue.com> to get commercial licensing options.
|
|
26
|
+
*/
|
|
4
27
|
const ClusterManager_1 = require("./ClusterManager");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
(function (MessageType) {
|
|
9
|
-
MessageType["Up"] = "up";
|
|
10
|
-
MessageType["Down"] = "down";
|
|
11
|
-
})(MessageType || (MessageType = {}));
|
|
28
|
+
const worker_threads_1 = require("worker_threads");
|
|
29
|
+
const path = require("path");
|
|
30
|
+
process.setMaxListeners(10000);
|
|
12
31
|
exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
aliveTimeoutCorrection:
|
|
32
|
+
port: 63000,
|
|
33
|
+
address: '255.255.255.255',
|
|
34
|
+
aliveTimeoutCorrection: 5000,
|
|
16
35
|
};
|
|
17
|
-
/**
|
|
18
|
-
* UDP broadcast-based cluster management implementation
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ~~~typescript
|
|
22
|
-
* const queue = new ClusteredRedisQueue('ClusteredQueue', {
|
|
23
|
-
* clusterManagers: [new UDPBroadcastClusterManager()],
|
|
24
|
-
* });
|
|
25
|
-
* ~~~
|
|
26
|
-
*/
|
|
27
36
|
class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
28
|
-
get socket() {
|
|
29
|
-
return UDPClusterManager.sockets[this.socketKey];
|
|
30
|
-
}
|
|
31
|
-
set socket(socket) {
|
|
32
|
-
UDPClusterManager.sockets[this.socketKey] = socket;
|
|
33
|
-
}
|
|
34
37
|
constructor(options) {
|
|
35
38
|
super();
|
|
36
39
|
this.options = Object.assign(Object.assign({}, exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS), options || {});
|
|
37
|
-
this.
|
|
40
|
+
this.startWorkerListener();
|
|
38
41
|
process.on('SIGTERM', UDPClusterManager.free);
|
|
39
42
|
process.on('SIGINT', UDPClusterManager.free);
|
|
40
43
|
process.on('SIGABRT', UDPClusterManager.free);
|
|
41
44
|
}
|
|
42
45
|
static async free() {
|
|
43
|
-
const
|
|
44
|
-
await Promise.all(
|
|
45
|
-
}
|
|
46
|
-
listenBroadcastedMessages(listener, options) {
|
|
47
|
-
const address = UDPClusterManager.selectNetworkInterface(options);
|
|
48
|
-
this.socketKey = `${address}:${options.broadcastPort}`;
|
|
49
|
-
if (!this.socket) {
|
|
50
|
-
this.socket = (0, dgram_1.createSocket)({
|
|
51
|
-
type: 'udp4',
|
|
52
|
-
reuseAddr: true,
|
|
53
|
-
reusePort: true,
|
|
54
|
-
}).bind(options.broadcastPort, address);
|
|
55
|
-
}
|
|
56
|
-
this.socket.on('message', message => {
|
|
57
|
-
listener(UDPClusterManager.parseBroadcastedMessage(message));
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
startListening() {
|
|
61
|
-
this.listenBroadcastedMessages(message => {
|
|
62
|
-
this.anyCluster(cluster => {
|
|
63
|
-
UDPClusterManager.processMessageOnCluster(cluster, message, this.options.aliveTimeoutCorrection);
|
|
64
|
-
}).then();
|
|
65
|
-
}, this.options);
|
|
66
|
-
}
|
|
67
|
-
static processMessageOnCluster(cluster, message, aliveTimeoutCorrection) {
|
|
68
|
-
const server = cluster.find(message);
|
|
69
|
-
if (server && message.type === MessageType.Down) {
|
|
70
|
-
return cluster.remove(message);
|
|
71
|
-
}
|
|
72
|
-
if (!server && message.type === MessageType.Up) {
|
|
73
|
-
return UDPClusterManager.serverAliveWait(cluster, cluster.add(message), message, aliveTimeoutCorrection, false);
|
|
74
|
-
}
|
|
75
|
-
if (server && message.type === MessageType.Up) {
|
|
76
|
-
UDPClusterManager.serverAliveWait(cluster, server, message, aliveTimeoutCorrection);
|
|
77
|
-
}
|
|
46
|
+
const workerKeys = Object.keys(UDPClusterManager.workers);
|
|
47
|
+
await Promise.all(workerKeys.map(workerKey => UDPClusterManager.destroyWorker(workerKey, UDPClusterManager.workers[workerKey])));
|
|
78
48
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
id,
|
|
84
|
-
name,
|
|
85
|
-
type: type.toLowerCase(),
|
|
86
|
-
host,
|
|
87
|
-
port: parseInt(port),
|
|
88
|
-
timeout: parseFloat(timeout) * 1000,
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
static serverAliveWait(cluster, server, message, aliveTimeoutCorrection, existingServer = true) {
|
|
92
|
-
if (!server) {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
if (server.timer === undefined && existingServer) {
|
|
49
|
+
startWorkerListener() {
|
|
50
|
+
this.workerKey = `${this.options.address}:${this.options.port}`;
|
|
51
|
+
if (UDPClusterManager.workers[this.workerKey]) {
|
|
52
|
+
this.worker = UDPClusterManager.workers[this.workerKey];
|
|
96
53
|
return;
|
|
97
54
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
if (
|
|
55
|
+
this.worker = new worker_threads_1.Worker(path.join(__dirname, './UDPWorker.js'), {
|
|
56
|
+
workerData: this.options,
|
|
57
|
+
});
|
|
58
|
+
this.worker.on('message', message => {
|
|
59
|
+
var _a;
|
|
60
|
+
const [className, method] = (_a = message.type) === null || _a === void 0 ? void 0 : _a.split(':');
|
|
61
|
+
if (className !== 'cluster') {
|
|
105
62
|
return;
|
|
106
63
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
64
|
+
return this.anyCluster(cluster => {
|
|
65
|
+
if (method === 'add') {
|
|
66
|
+
try {
|
|
67
|
+
const existing = typeof cluster.find === 'function'
|
|
68
|
+
? cluster.find(message.server, true)
|
|
69
|
+
: undefined;
|
|
70
|
+
if (existing) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch ( /* ignore */_a) { /* ignore */ }
|
|
75
|
+
}
|
|
76
|
+
const clusterMethod = cluster[method];
|
|
77
|
+
if (!clusterMethod) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
clusterMethod(message.server);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
UDPClusterManager.workers[this.workerKey] = this.worker;
|
|
115
84
|
}
|
|
116
|
-
/**
|
|
117
|
-
* Destroys the UDPClusterManager by closing all opened network connections
|
|
118
|
-
* and safely destroying all blocking sockets
|
|
119
|
-
*
|
|
120
|
-
* @returns {Promise<void>}
|
|
121
|
-
* @throws {Error}
|
|
122
|
-
*/
|
|
123
85
|
async destroy() {
|
|
124
|
-
await UDPClusterManager.
|
|
86
|
+
await UDPClusterManager.destroyWorker(this.workerKey, this.worker);
|
|
125
87
|
}
|
|
126
|
-
static async destroySocket(
|
|
88
|
+
static async destroySocket(key, socket) {
|
|
127
89
|
if (!socket) {
|
|
128
90
|
return;
|
|
129
91
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (typeof socket.close !== 'function') {
|
|
133
|
-
resolve();
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
92
|
+
try {
|
|
93
|
+
if (typeof socket.removeAllListeners === 'function') {
|
|
136
94
|
socket.removeAllListeners();
|
|
137
|
-
socket.close(() => {
|
|
138
|
-
if (socket && typeof socket.unref === 'function') {
|
|
139
|
-
socket.unref();
|
|
140
|
-
}
|
|
141
|
-
if (socketKey
|
|
142
|
-
&& UDPClusterManager.sockets[socketKey]) {
|
|
143
|
-
delete UDPClusterManager.sockets[socketKey];
|
|
144
|
-
}
|
|
145
|
-
resolve();
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
catch (e) {
|
|
149
|
-
reject(e);
|
|
150
95
|
}
|
|
96
|
+
}
|
|
97
|
+
catch (e) {
|
|
98
|
+
throw e;
|
|
99
|
+
}
|
|
100
|
+
if (typeof socket.close !== 'function') {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
await new Promise((resolve) => {
|
|
104
|
+
socket.close(() => {
|
|
105
|
+
if (typeof socket.unref === 'function') {
|
|
106
|
+
socket.unref();
|
|
107
|
+
}
|
|
108
|
+
if (UDPClusterManager.sockets && key in UDPClusterManager.sockets) {
|
|
109
|
+
delete UDPClusterManager.sockets[key];
|
|
110
|
+
}
|
|
111
|
+
resolve();
|
|
112
|
+
});
|
|
151
113
|
});
|
|
152
114
|
}
|
|
153
|
-
static
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const broadcastAddress = options.broadcastAddress
|
|
157
|
-
|| limitedBroadcastAddress;
|
|
158
|
-
const defaultAddress = '0.0.0.0';
|
|
159
|
-
if (!broadcastAddress || broadcastAddress === limitedBroadcastAddress) {
|
|
160
|
-
return defaultAddress;
|
|
115
|
+
static async destroyWorker(workerKey, worker) {
|
|
116
|
+
if (!worker) {
|
|
117
|
+
return;
|
|
161
118
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
if (
|
|
170
|
-
|
|
119
|
+
return new Promise(resolve => {
|
|
120
|
+
const timeout = setTimeout(() => {
|
|
121
|
+
worker.terminate();
|
|
122
|
+
resolve();
|
|
123
|
+
}, 5000);
|
|
124
|
+
worker.postMessage({ type: 'stop' });
|
|
125
|
+
worker.once('message', (message) => {
|
|
126
|
+
if (message.type === 'stopped') {
|
|
127
|
+
clearTimeout(timeout);
|
|
128
|
+
worker.terminate();
|
|
129
|
+
delete UDPClusterManager.workers[workerKey];
|
|
130
|
+
resolve();
|
|
171
131
|
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return defaultAddress;
|
|
132
|
+
});
|
|
133
|
+
});
|
|
175
134
|
}
|
|
176
135
|
}
|
|
177
136
|
exports.UDPClusterManager = UDPClusterManager;
|
|
137
|
+
UDPClusterManager.workers = {};
|
|
178
138
|
UDPClusterManager.sockets = {};
|
|
179
139
|
//# sourceMappingURL=UDPClusterManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/src/UDPWorker.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/*!
|
|
4
|
+
* UDP message listener for cluster managing: Worker for processing
|
|
5
|
+
* messages
|
|
6
|
+
*
|
|
7
|
+
* I'm Queue Software Project
|
|
8
|
+
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
9
|
+
*
|
|
10
|
+
* This program is free software: you can redistribute it and/or modify
|
|
11
|
+
* it under the terms of the GNU General Public License as published by
|
|
12
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
13
|
+
* (at your option) any later version.
|
|
14
|
+
*
|
|
15
|
+
* This program is distributed in the hope that it will be useful,
|
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18
|
+
* GNU General Public License for more details.
|
|
19
|
+
*
|
|
20
|
+
* You should have received a copy of the GNU General Public License
|
|
21
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
22
|
+
*
|
|
23
|
+
* If you want to use this code in a closed source (commercial) project, you can
|
|
24
|
+
* purchase a proprietary commercial license. Please contact us at
|
|
25
|
+
* <support@imqueue.com> to get commercial licensing options.
|
|
26
|
+
*/
|
|
27
|
+
const worker_threads_1 = require("worker_threads");
|
|
28
|
+
const dgram_1 = require("dgram");
|
|
29
|
+
const os_1 = require("os");
|
|
30
|
+
const uuid_1 = require("./uuid");
|
|
31
|
+
process.setMaxListeners(10000);
|
|
32
|
+
var MessageType;
|
|
33
|
+
(function (MessageType) {
|
|
34
|
+
MessageType["Up"] = "up";
|
|
35
|
+
MessageType["Down"] = "down";
|
|
36
|
+
})(MessageType || (MessageType = {}));
|
|
37
|
+
class UDPClusterWorker {
|
|
38
|
+
constructor(options, messagePort) {
|
|
39
|
+
this.options = options;
|
|
40
|
+
this.messagePort = messagePort;
|
|
41
|
+
this.servers = new Map();
|
|
42
|
+
this.setupMessageHandlers();
|
|
43
|
+
this.setupProcessHandlers();
|
|
44
|
+
this.socket = (0, dgram_1.createSocket)({
|
|
45
|
+
type: 'udp4',
|
|
46
|
+
reuseAddr: true,
|
|
47
|
+
reusePort: true,
|
|
48
|
+
}).bind(this.options.port, this.selectNetworkInterface());
|
|
49
|
+
this.socket.on('message', message => this.processMessage(this.parseMessage(message)));
|
|
50
|
+
}
|
|
51
|
+
static getServerKey(message) {
|
|
52
|
+
return message.id;
|
|
53
|
+
}
|
|
54
|
+
setupMessageHandlers() {
|
|
55
|
+
this.messagePort.on('message', message => {
|
|
56
|
+
if (message.type === 'stop') {
|
|
57
|
+
this.stop();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
setupProcessHandlers() {
|
|
62
|
+
process.on('SIGTERM', this.cleanup);
|
|
63
|
+
process.on('SIGINT', this.cleanup);
|
|
64
|
+
process.on('SIGABRT', this.cleanup);
|
|
65
|
+
}
|
|
66
|
+
addServer(message) {
|
|
67
|
+
this.messagePort.postMessage({
|
|
68
|
+
type: 'cluster:add',
|
|
69
|
+
server: UDPClusterWorker.mapMessage(message),
|
|
70
|
+
});
|
|
71
|
+
this.serverAliveWait(message);
|
|
72
|
+
}
|
|
73
|
+
removeServer(message) {
|
|
74
|
+
this.servers.delete(UDPClusterWorker.getServerKey(message));
|
|
75
|
+
this.messagePort.postMessage({
|
|
76
|
+
type: 'cluster:remove',
|
|
77
|
+
server: UDPClusterWorker.mapMessage(message),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
static mapMessage(message) {
|
|
81
|
+
return {
|
|
82
|
+
id: message.id,
|
|
83
|
+
name: message.name,
|
|
84
|
+
type: message.type,
|
|
85
|
+
host: message.host,
|
|
86
|
+
port: message.port,
|
|
87
|
+
timeout: message.timeout,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
serverAliveWait(message) {
|
|
91
|
+
var _a;
|
|
92
|
+
const stamp = (0, uuid_1.uuid)();
|
|
93
|
+
const correction = (_a = this.options.aliveTimeoutCorrection) !== null && _a !== void 0 ? _a : 0;
|
|
94
|
+
const effectiveTimeout = message.timeout + correction + 1;
|
|
95
|
+
const key = UDPClusterWorker.getServerKey(message);
|
|
96
|
+
this.servers.set(key, stamp);
|
|
97
|
+
const t = setTimeout(() => setImmediate(() => {
|
|
98
|
+
if (this.servers.get(key) === stamp) {
|
|
99
|
+
this.removeServer(message);
|
|
100
|
+
}
|
|
101
|
+
}), effectiveTimeout);
|
|
102
|
+
// Avoid keeping the event loop alive due to pending timers
|
|
103
|
+
try {
|
|
104
|
+
if (t && typeof t.unref === 'function') {
|
|
105
|
+
t.unref();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch ( /* ignore */_b) { /* ignore */ }
|
|
109
|
+
}
|
|
110
|
+
processMessage(message) {
|
|
111
|
+
if (message.type === MessageType.Down) {
|
|
112
|
+
return this.removeServer(message);
|
|
113
|
+
}
|
|
114
|
+
if (message.type === MessageType.Up) {
|
|
115
|
+
return this.addServer(message);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
selectNetworkInterface() {
|
|
119
|
+
const interfaces = (0, os_1.networkInterfaces)();
|
|
120
|
+
const broadcastAddress = this.options.address
|
|
121
|
+
|| this.options.limitedAddress;
|
|
122
|
+
const defaultAddress = '0.0.0.0';
|
|
123
|
+
if (!broadcastAddress
|
|
124
|
+
|| broadcastAddress === this.options.limitedAddress) {
|
|
125
|
+
return defaultAddress;
|
|
126
|
+
}
|
|
127
|
+
for (const key in interfaces) {
|
|
128
|
+
if (!interfaces[key]) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
for (const net of interfaces[key]) {
|
|
132
|
+
const shouldBeSelected = net.family === 'IPv4'
|
|
133
|
+
&& net.address.startsWith(broadcastAddress.replace(/\.255/g, ''));
|
|
134
|
+
if (shouldBeSelected) {
|
|
135
|
+
return net.address;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return defaultAddress;
|
|
140
|
+
}
|
|
141
|
+
parseMessage(input) {
|
|
142
|
+
const [name, id, type, address = '', timeout = '0',] = input.toString().split('\t');
|
|
143
|
+
const [host, port] = address.split(':');
|
|
144
|
+
return {
|
|
145
|
+
id,
|
|
146
|
+
name,
|
|
147
|
+
type: type.toLowerCase(),
|
|
148
|
+
host,
|
|
149
|
+
port: parseInt(port),
|
|
150
|
+
timeout: parseFloat(timeout) * 1000,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
stop() {
|
|
154
|
+
this.cleanup();
|
|
155
|
+
if (this.socket) {
|
|
156
|
+
this.socket.close(() => {
|
|
157
|
+
this.messagePort.postMessage({ type: 'stopped' });
|
|
158
|
+
});
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
this.messagePort.postMessage({ type: 'stopped' });
|
|
162
|
+
}
|
|
163
|
+
cleanup() {
|
|
164
|
+
this.servers.clear();
|
|
165
|
+
if (this.socket) {
|
|
166
|
+
this.socket.removeAllListeners();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (!worker_threads_1.isMainThread && worker_threads_1.parentPort) {
|
|
171
|
+
new UDPClusterWorker(worker_threads_1.workerData, worker_threads_1.parentPort);
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=UDPWorker.js.map
|