@imqueue/core 2.0.26 → 3.1.0
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/README.md +73 -63
- package/index.d.ts +0 -1
- package/index.js +8 -9
- package/package.json +19 -71
- package/src/ClusterManager.d.ts +12 -3
- package/src/ClusterManager.js +21 -10
- package/src/ClusteredRedisQueue.d.ts +45 -14
- package/src/ClusteredRedisQueue.js +186 -168
- package/src/IMessageQueue.d.ts +89 -96
- package/src/IMessageQueue.js +2 -2
- package/src/RedisQueue.d.ts +193 -97
- package/src/RedisQueue.js +1328 -1068
- package/src/UDPClusterManager.d.ts +100 -26
- package/src/UDPClusterManager.js +225 -70
- package/src/UDPWorker.d.ts +53 -1
- package/src/UDPWorker.js +79 -40
- package/src/{promisify.d.ts → helpers/buildOptions.d.ts} +8 -15
- package/src/helpers/buildOptions.js +39 -0
- package/src/{copyEventEmitter.d.ts → helpers/copyEventEmitter.d.ts} +1 -1
- package/src/{copyEventEmitter.js → helpers/copyEventEmitter.js} +3 -4
- package/src/helpers/envInt.d.ts +32 -0
- package/src/helpers/envInt.js +39 -0
- package/src/helpers/escapeRegExp.d.ts +30 -0
- package/src/helpers/escapeRegExp.js +36 -0
- package/src/helpers/index.d.ts +31 -0
- package/src/helpers/index.js +48 -0
- package/src/helpers/pack.d.ts +7 -0
- package/src/helpers/pack.js +37 -0
- package/src/helpers/randomInt.d.ts +31 -0
- package/src/helpers/randomInt.js +37 -0
- package/src/helpers/sha1.d.ts +7 -0
- package/src/helpers/sha1.js +39 -0
- package/src/helpers/unpack.d.ts +7 -0
- package/src/helpers/unpack.js +37 -0
- package/src/index.d.ts +1 -14
- package/src/index.js +1 -19
- package/src/profile.d.ts +28 -38
- package/src/profile.js +100 -95
- package/src/redis.d.ts +1 -1
- package/src/redis.js +4 -2
- package/debug.d.ts +0 -1
- package/debug_getter.d.ts +0 -9
- package/debug_listener.d.ts +0 -1
- package/eslint.config.mjs +0 -98
- package/src/promisify.js +0 -106
- package/src/uuid.d.ts +0 -6
- package/src/uuid.js +0 -65
|
@@ -1,27 +1,5 @@
|
|
|
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
|
-
*/
|
|
24
1
|
import { ClusterManager } from './ClusterManager';
|
|
2
|
+
import { ILogger } from './IMessageQueue';
|
|
25
3
|
export interface UDPClusterManagerOptions {
|
|
26
4
|
/**
|
|
27
5
|
* Message queue broadcast port
|
|
@@ -37,7 +15,7 @@ export interface UDPClusterManagerOptions {
|
|
|
37
15
|
*/
|
|
38
16
|
address: string;
|
|
39
17
|
/**
|
|
40
|
-
* Message
|
|
18
|
+
* Message-queue-limited broadcast address
|
|
41
19
|
*
|
|
42
20
|
* @default "255.255.255.255"
|
|
43
21
|
* @type {string}
|
|
@@ -61,18 +39,114 @@ export interface UDPClusterManagerOptions {
|
|
|
61
39
|
* @type {boolean}
|
|
62
40
|
*/
|
|
63
41
|
useAliveCheck: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Enable process signal handling (SIGTERM, SIGINT, SIGABRT) by the
|
|
44
|
+
* manager. When enabled, the manager stops its UDP workers on these
|
|
45
|
+
* signals and then re-raises the signal, so the process terminates
|
|
46
|
+
* through the default signal behavior. Disable if the host application
|
|
47
|
+
* manages its own shutdown sequence.
|
|
48
|
+
*
|
|
49
|
+
* @default true
|
|
50
|
+
* @type {boolean}
|
|
51
|
+
*/
|
|
52
|
+
handleSignals: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Logger used for worker supervision messages
|
|
55
|
+
*
|
|
56
|
+
* @default console
|
|
57
|
+
* @type {ILogger}
|
|
58
|
+
*/
|
|
59
|
+
logger: ILogger;
|
|
64
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Subset of the manager options handed over to the UDP worker thread. Only
|
|
63
|
+
* structured-cloneable values may appear here (no logger).
|
|
64
|
+
*/
|
|
65
|
+
export type UDPWorkerOptions = Omit<UDPClusterManagerOptions, 'logger' | 'handleSignals'>;
|
|
65
66
|
export declare const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS: UDPClusterManagerOptions;
|
|
66
67
|
export declare class UDPClusterManager extends ClusterManager {
|
|
67
68
|
private static workers;
|
|
68
|
-
|
|
69
|
+
/** Number of manager instances sharing each worker (by worker key) */
|
|
70
|
+
private static workerRefs;
|
|
71
|
+
/** Live manager instances per worker key, used for re-attachment */
|
|
72
|
+
private static instances;
|
|
73
|
+
/** True once process-level signal handlers have been registered */
|
|
74
|
+
private static signalsBound;
|
|
75
|
+
/** True while the process is shutting down via a signal */
|
|
76
|
+
private static shuttingDown;
|
|
77
|
+
/**
|
|
78
|
+
* Workers we terminated on purpose. worker.terminate() makes the 'exit'
|
|
79
|
+
* event fire with a non-zero code, which would otherwise be mistaken for
|
|
80
|
+
* a crash and trigger a spurious "exited unexpectedly" warning and respawn.
|
|
81
|
+
*/
|
|
82
|
+
private static intentionallyStopped;
|
|
69
83
|
private readonly options;
|
|
70
84
|
private workerKey;
|
|
71
85
|
private worker;
|
|
86
|
+
private destroyed;
|
|
72
87
|
constructor(options?: Partial<UDPClusterManagerOptions>);
|
|
88
|
+
private get logger();
|
|
89
|
+
/**
|
|
90
|
+
* Builds the shared-worker key from every option the worker thread
|
|
91
|
+
* depends on, so managers configured differently never silently share
|
|
92
|
+
* a worker built from another manager's options.
|
|
93
|
+
*
|
|
94
|
+
* @param {UDPClusterManagerOptions} options
|
|
95
|
+
* @returns {string}
|
|
96
|
+
*/
|
|
97
|
+
private static workerKeyFor;
|
|
98
|
+
/**
|
|
99
|
+
* Registers process-level shutdown handlers exactly once per process.
|
|
100
|
+
* After stopping the workers, the original signal is re-raised, so the
|
|
101
|
+
* default termination behavior (which registering a handler cancels)
|
|
102
|
+
* still applies, and the process exits.
|
|
103
|
+
*/
|
|
104
|
+
private static bindSignals;
|
|
105
|
+
/**
|
|
106
|
+
* Stops all workers and re-raises the given signal, so the process
|
|
107
|
+
* terminates through the default signal behavior.
|
|
108
|
+
*
|
|
109
|
+
* @param {NodeJS.Signals} signal
|
|
110
|
+
* @returns {Promise<void>}
|
|
111
|
+
*/
|
|
112
|
+
private static freeAndRaise;
|
|
73
113
|
private static free;
|
|
114
|
+
/**
|
|
115
|
+
* Registers this instance on the (possibly shared) worker for its
|
|
116
|
+
* options. Every instance attaches its own message listener, so all
|
|
117
|
+
* managers sharing a worker receive cluster updates.
|
|
118
|
+
*/
|
|
74
119
|
private startWorkerListener;
|
|
120
|
+
/**
|
|
121
|
+
* Spawns and supervises a UDP worker for this manager's options. On an
|
|
122
|
+
* unexpected worker death the worker is dropped from the registry, and
|
|
123
|
+
* a re-spawn is scheduled while live manager instances remain.
|
|
124
|
+
*
|
|
125
|
+
* @returns {Worker}
|
|
126
|
+
*/
|
|
127
|
+
private spawnWorker;
|
|
128
|
+
/**
|
|
129
|
+
* Schedules a replacement worker for the given key and re-attaches all
|
|
130
|
+
* live manager instances to it, so cluster membership does not silently
|
|
131
|
+
* freeze after a worker crash.
|
|
132
|
+
*
|
|
133
|
+
* @param {string} workerKey
|
|
134
|
+
*/
|
|
135
|
+
private static respawn;
|
|
136
|
+
/**
|
|
137
|
+
* Bound per-instance worker message listener, kept as a field so it can
|
|
138
|
+
* be detached from the shared worker when this instance is destroyed.
|
|
139
|
+
*/
|
|
140
|
+
private readonly onWorkerMessage;
|
|
141
|
+
/**
|
|
142
|
+
* Applies a worker cluster message (add/remove) to every registered
|
|
143
|
+
* cluster. Cluster callback errors are contained per cluster, so the
|
|
144
|
+
* worker message listener can never raise an unhandled rejection.
|
|
145
|
+
*
|
|
146
|
+
* @param {WorkerMessage} message
|
|
147
|
+
* @returns {Promise<void>}
|
|
148
|
+
*/
|
|
149
|
+
private handleWorkerMessage;
|
|
75
150
|
destroy(): Promise<void>;
|
|
76
|
-
static destroySocket(key: string, socket?: any): Promise<void>;
|
|
77
151
|
private static destroyWorker;
|
|
78
152
|
}
|
package/src/UDPClusterManager.js
CHANGED
|
@@ -24,118 +24,273 @@ exports.UDPClusterManager = exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = void 0
|
|
|
24
24
|
* purchase a proprietary commercial license. Please contact us at
|
|
25
25
|
* <support@imqueue.com> to get commercial licensing options.
|
|
26
26
|
*/
|
|
27
|
+
const node_path_1 = require("node:path");
|
|
28
|
+
const node_worker_threads_1 = require("node:worker_threads");
|
|
27
29
|
const ClusterManager_1 = require("./ClusterManager");
|
|
28
|
-
const worker_threads_1 = require("worker_threads");
|
|
29
|
-
const path = require("path");
|
|
30
|
-
process.setMaxListeners(10000);
|
|
31
30
|
const IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK = !!+(process.env.IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK || 1);
|
|
31
|
+
/** Delay (ms) before an unexpectedly dead worker is re-spawned */
|
|
32
|
+
const WORKER_RESPAWN_DELAY = 1000;
|
|
32
33
|
exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = {
|
|
33
34
|
port: 63000,
|
|
34
35
|
address: '255.255.255.255',
|
|
35
36
|
aliveTimeoutCorrection: 5000,
|
|
36
37
|
useAliveCheck: IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK,
|
|
38
|
+
handleSignals: true,
|
|
39
|
+
logger: console,
|
|
37
40
|
};
|
|
38
41
|
class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
42
|
+
static workers = {};
|
|
43
|
+
/** Number of manager instances sharing each worker (by worker key) */
|
|
44
|
+
static workerRefs = {};
|
|
45
|
+
/** Live manager instances per worker key, used for re-attachment */
|
|
46
|
+
static instances = {};
|
|
47
|
+
/** True once process-level signal handlers have been registered */
|
|
48
|
+
static signalsBound = false;
|
|
49
|
+
/** True while the process is shutting down via a signal */
|
|
50
|
+
static shuttingDown = false;
|
|
51
|
+
/**
|
|
52
|
+
* Workers we terminated on purpose. worker.terminate() makes the 'exit'
|
|
53
|
+
* event fire with a non-zero code, which would otherwise be mistaken for
|
|
54
|
+
* a crash and trigger a spurious "exited unexpectedly" warning and respawn.
|
|
55
|
+
*/
|
|
56
|
+
static intentionallyStopped = new WeakSet();
|
|
57
|
+
options;
|
|
58
|
+
workerKey;
|
|
59
|
+
worker;
|
|
60
|
+
destroyed = false;
|
|
39
61
|
constructor(options) {
|
|
40
62
|
super();
|
|
41
|
-
this.options =
|
|
63
|
+
this.options = {
|
|
64
|
+
...exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS,
|
|
65
|
+
...options,
|
|
66
|
+
};
|
|
42
67
|
this.startWorkerListener();
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
68
|
+
if (this.options.handleSignals) {
|
|
69
|
+
UDPClusterManager.bindSignals();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
get logger() {
|
|
73
|
+
return this.options.logger;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Builds the shared-worker key from every option the worker thread
|
|
77
|
+
* depends on, so managers configured differently never silently share
|
|
78
|
+
* a worker built from another manager's options.
|
|
79
|
+
*
|
|
80
|
+
* @param {UDPClusterManagerOptions} options
|
|
81
|
+
* @returns {string}
|
|
82
|
+
*/
|
|
83
|
+
static workerKeyFor(options) {
|
|
84
|
+
return [
|
|
85
|
+
options.address,
|
|
86
|
+
options.port,
|
|
87
|
+
options.limitedAddress ?? '',
|
|
88
|
+
options.aliveTimeoutCorrection,
|
|
89
|
+
options.useAliveCheck,
|
|
90
|
+
].join('|');
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Registers process-level shutdown handlers exactly once per process.
|
|
94
|
+
* After stopping the workers, the original signal is re-raised, so the
|
|
95
|
+
* default termination behavior (which registering a handler cancels)
|
|
96
|
+
* still applies, and the process exits.
|
|
97
|
+
*/
|
|
98
|
+
static bindSignals() {
|
|
99
|
+
if (UDPClusterManager.signalsBound) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
UDPClusterManager.signalsBound = true;
|
|
103
|
+
const onSignal = (signal) => {
|
|
104
|
+
void UDPClusterManager.freeAndRaise(signal);
|
|
105
|
+
};
|
|
106
|
+
process.once('SIGTERM', onSignal);
|
|
107
|
+
process.once('SIGINT', onSignal);
|
|
108
|
+
process.once('SIGABRT', onSignal);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Stops all workers and re-raises the given signal, so the process
|
|
112
|
+
* terminates through the default signal behavior.
|
|
113
|
+
*
|
|
114
|
+
* @param {NodeJS.Signals} signal
|
|
115
|
+
* @returns {Promise<void>}
|
|
116
|
+
*/
|
|
117
|
+
static async freeAndRaise(signal) {
|
|
118
|
+
await UDPClusterManager.free();
|
|
119
|
+
// the once-registered handler is already removed at this point, so
|
|
120
|
+
// re-raising hits the default handler and terminates the process
|
|
121
|
+
process.kill(process.pid, signal);
|
|
46
122
|
}
|
|
47
123
|
static async free() {
|
|
124
|
+
UDPClusterManager.shuttingDown = true;
|
|
48
125
|
const workerKeys = Object.keys(UDPClusterManager.workers);
|
|
49
126
|
await Promise.all(workerKeys.map(workerKey => UDPClusterManager.destroyWorker(workerKey, UDPClusterManager.workers[workerKey])));
|
|
50
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Registers this instance on the (possibly shared) worker for its
|
|
130
|
+
* options. Every instance attaches its own message listener, so all
|
|
131
|
+
* managers sharing a worker receive cluster updates.
|
|
132
|
+
*/
|
|
51
133
|
startWorkerListener() {
|
|
52
|
-
this.workerKey =
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
134
|
+
this.workerKey = UDPClusterManager.workerKeyFor(this.options);
|
|
135
|
+
UDPClusterManager.workerRefs[this.workerKey] =
|
|
136
|
+
(UDPClusterManager.workerRefs[this.workerKey] || 0) + 1;
|
|
137
|
+
(UDPClusterManager.instances[this.workerKey] ??= new Set()).add(this);
|
|
138
|
+
this.worker =
|
|
139
|
+
UDPClusterManager.workers[this.workerKey] || this.spawnWorker();
|
|
140
|
+
this.worker.on('message', this.onWorkerMessage);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Spawns and supervises a UDP worker for this manager's options. On an
|
|
144
|
+
* unexpected worker death the worker is dropped from the registry, and
|
|
145
|
+
* a re-spawn is scheduled while live manager instances remain.
|
|
146
|
+
*
|
|
147
|
+
* @returns {Worker}
|
|
148
|
+
*/
|
|
149
|
+
spawnWorker() {
|
|
150
|
+
const workerData = {
|
|
151
|
+
port: this.options.port,
|
|
152
|
+
address: this.options.address,
|
|
153
|
+
limitedAddress: this.options.limitedAddress,
|
|
154
|
+
aliveTimeoutCorrection: this.options.aliveTimeoutCorrection,
|
|
155
|
+
useAliveCheck: this.options.useAliveCheck,
|
|
156
|
+
};
|
|
157
|
+
const worker = new node_worker_threads_1.Worker((0, node_path_1.join)(__dirname, './UDPWorker.js'), {
|
|
158
|
+
workerData,
|
|
59
159
|
});
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
160
|
+
const workerKey = this.workerKey;
|
|
161
|
+
// many manager instances may listen on one shared worker
|
|
162
|
+
worker.setMaxListeners(0);
|
|
163
|
+
worker.on('error', err => {
|
|
164
|
+
this.logger.error(`UDPClusterManager: worker ${workerKey} error:`, err);
|
|
165
|
+
});
|
|
166
|
+
worker.on('exit', code => {
|
|
167
|
+
if (UDPClusterManager.workers[workerKey] === worker) {
|
|
168
|
+
delete UDPClusterManager.workers[workerKey];
|
|
169
|
+
}
|
|
170
|
+
// an exit we caused via terminate() (graceful destroy/shutdown)
|
|
171
|
+
// is expected, even though terminate() reports a non-zero code
|
|
172
|
+
if (UDPClusterManager.intentionallyStopped.has(worker)) {
|
|
173
|
+
UDPClusterManager.intentionallyStopped.delete(worker);
|
|
64
174
|
return;
|
|
65
175
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
: undefined;
|
|
72
|
-
if (existing) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
catch ( /* ignore */_a) { /* ignore */ }
|
|
77
|
-
}
|
|
78
|
-
const clusterMethod = cluster[method];
|
|
79
|
-
if (!clusterMethod) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
clusterMethod(message.server);
|
|
83
|
-
});
|
|
176
|
+
if (code !== 0 && !UDPClusterManager.shuttingDown) {
|
|
177
|
+
this.logger.warn(`UDPClusterManager: worker ${workerKey} exited ` +
|
|
178
|
+
`unexpectedly (code ${code})`);
|
|
179
|
+
UDPClusterManager.respawn(workerKey);
|
|
180
|
+
}
|
|
84
181
|
});
|
|
85
|
-
UDPClusterManager.workers[this.workerKey] =
|
|
182
|
+
UDPClusterManager.workers[this.workerKey] = worker;
|
|
183
|
+
return worker;
|
|
86
184
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
185
|
+
/**
|
|
186
|
+
* Schedules a replacement worker for the given key and re-attaches all
|
|
187
|
+
* live manager instances to it, so cluster membership does not silently
|
|
188
|
+
* freeze after a worker crash.
|
|
189
|
+
*
|
|
190
|
+
* @param {string} workerKey
|
|
191
|
+
*/
|
|
192
|
+
static respawn(workerKey) {
|
|
193
|
+
const instances = UDPClusterManager.instances[workerKey];
|
|
194
|
+
if (UDPClusterManager.shuttingDown || !instances?.size) {
|
|
92
195
|
return;
|
|
93
196
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
197
|
+
const timer = setTimeout(() => {
|
|
198
|
+
const [first] = instances;
|
|
199
|
+
if (!first ||
|
|
200
|
+
UDPClusterManager.shuttingDown ||
|
|
201
|
+
UDPClusterManager.workers[workerKey]) {
|
|
202
|
+
return;
|
|
97
203
|
}
|
|
204
|
+
const worker = first.spawnWorker();
|
|
205
|
+
for (const instance of instances) {
|
|
206
|
+
instance.worker = worker;
|
|
207
|
+
worker.on('message', instance.onWorkerMessage);
|
|
208
|
+
}
|
|
209
|
+
}, WORKER_RESPAWN_DELAY);
|
|
210
|
+
timer.unref();
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Bound per-instance worker message listener, kept as a field so it can
|
|
214
|
+
* be detached from the shared worker when this instance is destroyed.
|
|
215
|
+
*/
|
|
216
|
+
onWorkerMessage = (message) => {
|
|
217
|
+
void this.handleWorkerMessage(message);
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
* Applies a worker cluster message (add/remove) to every registered
|
|
221
|
+
* cluster. Cluster callback errors are contained per cluster, so the
|
|
222
|
+
* worker message listener can never raise an unhandled rejection.
|
|
223
|
+
*
|
|
224
|
+
* @param {WorkerMessage} message
|
|
225
|
+
* @returns {Promise<void>}
|
|
226
|
+
*/
|
|
227
|
+
async handleWorkerMessage(message) {
|
|
228
|
+
if (message.type === 'error') {
|
|
229
|
+
this.logger.warn(`UDPClusterManager: worker socket error: ${message.error}`);
|
|
230
|
+
return;
|
|
98
231
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
if (typeof socket.close !== 'function') {
|
|
232
|
+
const [className, method] = String(message.type ?? '').split(':');
|
|
233
|
+
if (className !== 'cluster') {
|
|
103
234
|
return;
|
|
104
235
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
resolve();
|
|
114
|
-
});
|
|
236
|
+
const action = method;
|
|
237
|
+
await this.forEachCluster(cluster => {
|
|
238
|
+
const server = message.server;
|
|
239
|
+
if (action === 'add' && cluster.find(server)) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const handler = cluster[action];
|
|
243
|
+
handler?.(server);
|
|
115
244
|
});
|
|
116
245
|
}
|
|
246
|
+
async destroy() {
|
|
247
|
+
if (this.destroyed) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
this.destroyed = true;
|
|
251
|
+
this.worker.off('message', this.onWorkerMessage);
|
|
252
|
+
UDPClusterManager.instances[this.workerKey]?.delete(this);
|
|
253
|
+
const refs = UDPClusterManager.workerRefs[this.workerKey] ?? 0;
|
|
254
|
+
if (refs > 1) {
|
|
255
|
+
// the worker is still shared with other manager instances
|
|
256
|
+
// configured the same way — just release this reference
|
|
257
|
+
UDPClusterManager.workerRefs[this.workerKey] = refs - 1;
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
delete UDPClusterManager.workerRefs[this.workerKey];
|
|
261
|
+
delete UDPClusterManager.instances[this.workerKey];
|
|
262
|
+
await UDPClusterManager.destroyWorker(this.workerKey, this.worker);
|
|
263
|
+
}
|
|
117
264
|
static async destroyWorker(workerKey, worker) {
|
|
118
265
|
if (!worker) {
|
|
119
266
|
return;
|
|
120
267
|
}
|
|
121
268
|
return new Promise(resolve => {
|
|
122
|
-
const
|
|
269
|
+
const finish = () => {
|
|
270
|
+
worker.off('message', onMessage);
|
|
271
|
+
clearTimeout(timeout);
|
|
272
|
+
// mark before terminating: the resulting non-zero exit is
|
|
273
|
+
// intentional and must not be treated as a crash
|
|
274
|
+
UDPClusterManager.intentionallyStopped.add(worker);
|
|
123
275
|
worker.terminate();
|
|
276
|
+
if (UDPClusterManager.workers[workerKey] === worker) {
|
|
277
|
+
delete UDPClusterManager.workers[workerKey];
|
|
278
|
+
}
|
|
124
279
|
resolve();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
280
|
+
};
|
|
281
|
+
// a persistent, filtering listener: unrelated cluster messages
|
|
282
|
+
// arriving between the stop request and the stop confirmation
|
|
283
|
+
// must not consume the wait
|
|
284
|
+
const onMessage = (message) => {
|
|
128
285
|
if (message.type === 'stopped') {
|
|
129
|
-
|
|
130
|
-
worker.terminate();
|
|
131
|
-
delete UDPClusterManager.workers[workerKey];
|
|
132
|
-
resolve();
|
|
286
|
+
finish();
|
|
133
287
|
}
|
|
134
|
-
}
|
|
288
|
+
};
|
|
289
|
+
const timeout = setTimeout(finish, 5000);
|
|
290
|
+
worker.on('message', onMessage);
|
|
291
|
+
worker.postMessage({ type: 'stop' });
|
|
135
292
|
});
|
|
136
293
|
}
|
|
137
294
|
}
|
|
138
295
|
exports.UDPClusterManager = UDPClusterManager;
|
|
139
|
-
UDPClusterManager.workers = {};
|
|
140
|
-
UDPClusterManager.sockets = {};
|
|
141
296
|
//# sourceMappingURL=UDPClusterManager.js.map
|
package/src/UDPWorker.d.ts
CHANGED
|
@@ -1 +1,53 @@
|
|
|
1
|
-
|
|
1
|
+
/*!
|
|
2
|
+
* UDP message listener for cluster managing: Worker for processing
|
|
3
|
+
* messages
|
|
4
|
+
*
|
|
5
|
+
* I'm Queue Software Project
|
|
6
|
+
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
7
|
+
*
|
|
8
|
+
* This program is free software: you can redistribute it and/or modify
|
|
9
|
+
* it under the terms of the GNU General Public License as published by
|
|
10
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
* (at your option) any later version.
|
|
12
|
+
*
|
|
13
|
+
* This program is distributed in the hope that it will be useful,
|
|
14
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
* GNU General Public License for more details.
|
|
17
|
+
*
|
|
18
|
+
* You should have received a copy of the GNU General Public License
|
|
19
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
20
|
+
*
|
|
21
|
+
* If you want to use this code in a closed source (commercial) project, you can
|
|
22
|
+
* purchase a proprietary commercial license. Please contact us at
|
|
23
|
+
* <support@imqueue.com> to get commercial licensing options.
|
|
24
|
+
*/
|
|
25
|
+
import { MessagePort } from 'node:worker_threads';
|
|
26
|
+
import { UDPWorkerOptions } from './UDPClusterManager';
|
|
27
|
+
export declare class UDPWorker {
|
|
28
|
+
private readonly options;
|
|
29
|
+
private readonly messagePort;
|
|
30
|
+
private readonly socket;
|
|
31
|
+
private readonly servers;
|
|
32
|
+
constructor(options: UDPWorkerOptions, messagePort: MessagePort);
|
|
33
|
+
private static getServerKey;
|
|
34
|
+
private setupMessageHandlers;
|
|
35
|
+
private addServer;
|
|
36
|
+
private removeServer;
|
|
37
|
+
private static mapMessage;
|
|
38
|
+
private serverAliveWait;
|
|
39
|
+
private processMessage;
|
|
40
|
+
private selectNetworkInterface;
|
|
41
|
+
/**
|
|
42
|
+
* Parses a raw broadcast datagram into a message. Returns null for
|
|
43
|
+
* malformed input (missing fields, non-numeric port, or timeout), so
|
|
44
|
+
* garbage on the broadcast port is dropped instead of producing
|
|
45
|
+
* NaN-driven timers or crashes.
|
|
46
|
+
*
|
|
47
|
+
* @param {Buffer} input
|
|
48
|
+
* @returns {Message | null}
|
|
49
|
+
*/
|
|
50
|
+
private parseMessage;
|
|
51
|
+
private stop;
|
|
52
|
+
private cleanup;
|
|
53
|
+
}
|