@imqueue/core 2.0.8 → 2.0.9
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
package/src/ClusterManager.d.ts
CHANGED
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
*/
|
|
24
24
|
import { IMessageQueueConnection, IServerInput } from './IMessageQueue';
|
|
25
25
|
export interface ICluster {
|
|
26
|
-
add: (server: IServerInput) =>
|
|
26
|
+
add: <T extends IMessageQueueConnection>(server: IServerInput) => T;
|
|
27
27
|
remove: (server: IServerInput) => void;
|
|
28
|
-
find: <T extends IMessageQueueConnection>(server: IServerInput
|
|
28
|
+
find: <T extends IMessageQueueConnection>(server: IServerInput) => T | undefined;
|
|
29
29
|
}
|
|
30
30
|
export interface InitializedCluster extends ICluster {
|
|
31
31
|
id: string;
|
|
@@ -22,7 +22,10 @@
|
|
|
22
22
|
* <support@imqueue.com> to get commercial licensing options.
|
|
23
23
|
*/
|
|
24
24
|
import { EventEmitter } from 'events';
|
|
25
|
-
import { EventMap, ILogger, IMessageQueue, IMQMode, IMQOptions, IServerInput, JsonObject } from '.';
|
|
25
|
+
import { EventMap, ILogger, IMessageQueue, IMessageQueueConnection, IMQMode, IMQOptions, IServerInput, JsonObject, RedisQueue } from '.';
|
|
26
|
+
interface ClusterServer extends IMessageQueueConnection {
|
|
27
|
+
imq?: RedisQueue;
|
|
28
|
+
}
|
|
26
29
|
/**
|
|
27
30
|
* Class ClusteredRedisQueue
|
|
28
31
|
* Implements the possibility to scale queues horizontally between several
|
|
@@ -173,7 +176,7 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
173
176
|
* @param {IServerInput} server
|
|
174
177
|
* @returns {void}
|
|
175
178
|
*/
|
|
176
|
-
protected addServer(server: IServerInput):
|
|
179
|
+
protected addServer(server: IServerInput): ClusterServer;
|
|
177
180
|
/**
|
|
178
181
|
* Removes server from the cluster
|
|
179
182
|
*
|
|
@@ -187,3 +190,4 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
187
190
|
private findServer;
|
|
188
191
|
private static matchServers;
|
|
189
192
|
}
|
|
193
|
+
export {};
|
|
@@ -367,7 +367,7 @@ class ClusteredRedisQueue {
|
|
|
367
367
|
* @returns {void}
|
|
368
368
|
*/
|
|
369
369
|
removeServer(server) {
|
|
370
|
-
const remove = this.findServer(server
|
|
370
|
+
const remove = this.findServer(server);
|
|
371
371
|
if (!remove) {
|
|
372
372
|
return;
|
|
373
373
|
}
|
|
@@ -403,6 +403,7 @@ class ClusteredRedisQueue {
|
|
|
403
403
|
this.servers.push(newServer);
|
|
404
404
|
this.clusterEmitter.emit('add', { server: newServer, imq });
|
|
405
405
|
this.queueLength = this.imqs.length;
|
|
406
|
+
return newServer;
|
|
406
407
|
}
|
|
407
408
|
eventEmitters() {
|
|
408
409
|
return [...this.imqs, this.templateEmitter];
|
|
@@ -416,19 +417,16 @@ class ClusteredRedisQueue {
|
|
|
416
417
|
await imq.subscribe(this.state.subscription.channel, this.state.subscription.handler);
|
|
417
418
|
}
|
|
418
419
|
}
|
|
419
|
-
findServer(server
|
|
420
|
-
return this.servers.find(existing => ClusteredRedisQueue.matchServers(existing, server
|
|
420
|
+
findServer(server) {
|
|
421
|
+
return this.servers.find(existing => ClusteredRedisQueue.matchServers(existing, server));
|
|
421
422
|
}
|
|
422
|
-
static matchServers(source, target
|
|
423
|
+
static matchServers(source, target) {
|
|
423
424
|
const sameAddress = target.host === source.host
|
|
424
425
|
&& target.port === source.port;
|
|
425
426
|
if (!target.id && !source.id) {
|
|
426
427
|
return sameAddress;
|
|
427
428
|
}
|
|
428
429
|
const sameId = target.id === source.id;
|
|
429
|
-
if (strict) {
|
|
430
|
-
return sameId && sameAddress;
|
|
431
|
-
}
|
|
432
430
|
return sameId || sameAddress;
|
|
433
431
|
}
|
|
434
432
|
}
|
|
@@ -11,14 +11,14 @@ export interface UDPClusterManagerOptions {
|
|
|
11
11
|
* @default 63000
|
|
12
12
|
* @type {number}
|
|
13
13
|
*/
|
|
14
|
-
broadcastPort
|
|
14
|
+
broadcastPort: number;
|
|
15
15
|
/**
|
|
16
16
|
* Message queue broadcast address
|
|
17
17
|
*
|
|
18
18
|
* @default limitedBroadcastAddress
|
|
19
19
|
* @type {number}
|
|
20
20
|
*/
|
|
21
|
-
broadcastAddress
|
|
21
|
+
broadcastAddress: string;
|
|
22
22
|
/**
|
|
23
23
|
* Message queue limited broadcast address
|
|
24
24
|
*
|
|
@@ -33,21 +33,7 @@ export interface UDPClusterManagerOptions {
|
|
|
33
33
|
* @default 1000
|
|
34
34
|
* @type {number}
|
|
35
35
|
*/
|
|
36
|
-
aliveTimeoutCorrection
|
|
37
|
-
/**
|
|
38
|
-
* Skip messages that are broadcast by specified addresses or set to
|
|
39
|
-
* "localhost" if you want to skip messages from "127.0.0.1" or "::1"
|
|
40
|
-
*
|
|
41
|
-
* @type {"local" | string[]}
|
|
42
|
-
*/
|
|
43
|
-
excludeHosts?: 'localhost' | string[];
|
|
44
|
-
/**
|
|
45
|
-
* Allow messages that are broadcast only by specified addresses or set to
|
|
46
|
-
* "localhost" if you want to allow messages only from "127.0.0.1" or "::1"
|
|
47
|
-
*
|
|
48
|
-
* @type {"local" | string[]}
|
|
49
|
-
*/
|
|
50
|
-
includeHosts?: 'localhost' | string[];
|
|
36
|
+
aliveTimeoutCorrection: number;
|
|
51
37
|
}
|
|
52
38
|
/**
|
|
53
39
|
* UDP broadcast-based cluster management implementation
|
|
@@ -65,13 +51,11 @@ export declare class UDPClusterManager extends ClusterManager {
|
|
|
65
51
|
private socketKey;
|
|
66
52
|
private get socket();
|
|
67
53
|
private set socket(value);
|
|
68
|
-
constructor(options?: UDPClusterManagerOptions);
|
|
54
|
+
constructor(options?: Partial<UDPClusterManagerOptions>);
|
|
69
55
|
private static free;
|
|
70
56
|
private listenBroadcastedMessages;
|
|
71
57
|
private startListening;
|
|
72
|
-
private static verifyHosts;
|
|
73
58
|
private static processMessageOnCluster;
|
|
74
|
-
private static processBroadcastedMessage;
|
|
75
59
|
private static parseBroadcastedMessage;
|
|
76
60
|
private static serverAliveWait;
|
|
77
61
|
/**
|
package/src/UDPClusterManager.js
CHANGED
|
@@ -12,13 +12,8 @@ var MessageType;
|
|
|
12
12
|
exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = {
|
|
13
13
|
broadcastPort: 63000,
|
|
14
14
|
broadcastAddress: '255.255.255.255',
|
|
15
|
-
aliveTimeoutCorrection:
|
|
15
|
+
aliveTimeoutCorrection: 1000,
|
|
16
16
|
};
|
|
17
|
-
const LOCALHOST_ADDRESSES = [
|
|
18
|
-
'localhost',
|
|
19
|
-
'127.0.0.1',
|
|
20
|
-
'::1',
|
|
21
|
-
];
|
|
22
17
|
/**
|
|
23
18
|
* UDP broadcast-based cluster management implementation
|
|
24
19
|
*
|
|
@@ -39,7 +34,7 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
39
34
|
constructor(options) {
|
|
40
35
|
super();
|
|
41
36
|
this.options = Object.assign(Object.assign({}, exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS), options || {});
|
|
42
|
-
this.startListening(
|
|
37
|
+
this.startListening();
|
|
43
38
|
process.on('SIGTERM', UDPClusterManager.free);
|
|
44
39
|
process.on('SIGINT', UDPClusterManager.free);
|
|
45
40
|
process.on('SIGABRT', UDPClusterManager.free);
|
|
@@ -52,53 +47,35 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
52
47
|
const address = UDPClusterManager.selectNetworkInterface(options);
|
|
53
48
|
this.socketKey = `${address}:${options.broadcastPort}`;
|
|
54
49
|
if (!this.socket) {
|
|
55
|
-
this.socket = (0, dgram_1.createSocket)({
|
|
56
|
-
|
|
50
|
+
this.socket = (0, dgram_1.createSocket)({
|
|
51
|
+
type: 'udp4',
|
|
52
|
+
reuseAddr: true,
|
|
53
|
+
reusePort: true,
|
|
54
|
+
}).bind(options.broadcastPort, address);
|
|
57
55
|
}
|
|
58
|
-
this.socket.on('message', message =>
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
this.listenBroadcastedMessages(UDPClusterManager.processBroadcastedMessage(this), options);
|
|
56
|
+
this.socket.on('message', message => {
|
|
57
|
+
listener(UDPClusterManager.parseBroadcastedMessage(message));
|
|
58
|
+
});
|
|
62
59
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
startListening() {
|
|
61
|
+
this.listenBroadcastedMessages(message => {
|
|
62
|
+
this.anyCluster(cluster => {
|
|
63
|
+
UDPClusterManager.processMessageOnCluster(cluster, message, this.options.aliveTimeoutCorrection);
|
|
64
|
+
}).then();
|
|
65
|
+
}, this.options);
|
|
68
66
|
}
|
|
69
67
|
static processMessageOnCluster(cluster, message, aliveTimeoutCorrection) {
|
|
70
68
|
const server = cluster.find(message);
|
|
71
69
|
if (server && message.type === MessageType.Down) {
|
|
72
|
-
clearTimeout(server.timer);
|
|
73
70
|
return cluster.remove(message);
|
|
74
71
|
}
|
|
75
72
|
if (!server && message.type === MessageType.Up) {
|
|
76
|
-
cluster.add(message);
|
|
77
|
-
const added = cluster.find(message, true);
|
|
78
|
-
if (added) {
|
|
79
|
-
UDPClusterManager.serverAliveWait(cluster, added, aliveTimeoutCorrection);
|
|
80
|
-
}
|
|
81
|
-
return;
|
|
73
|
+
return UDPClusterManager.serverAliveWait(cluster, cluster.add(message), message, aliveTimeoutCorrection, false);
|
|
82
74
|
}
|
|
83
75
|
if (server && message.type === MessageType.Up) {
|
|
84
|
-
|
|
76
|
+
UDPClusterManager.serverAliveWait(cluster, server, message, aliveTimeoutCorrection);
|
|
85
77
|
}
|
|
86
78
|
}
|
|
87
|
-
static processBroadcastedMessage(context) {
|
|
88
|
-
return message => {
|
|
89
|
-
if (context.options.excludeHosts
|
|
90
|
-
&& UDPClusterManager.verifyHosts(message.host, context.options.excludeHosts)) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
if (context.options.includeHosts
|
|
94
|
-
&& !UDPClusterManager.verifyHosts(message.host, context.options.includeHosts)) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
context.anyCluster(cluster => {
|
|
98
|
-
UDPClusterManager.processMessageOnCluster(cluster, message, context.options.aliveTimeoutCorrection);
|
|
99
|
-
}).then();
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
79
|
static parseBroadcastedMessage(input) {
|
|
103
80
|
const [name, id, type, address = '', timeout = '0',] = input.toString().split('\t');
|
|
104
81
|
const [host, port] = address.split(':');
|
|
@@ -111,27 +88,30 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
111
88
|
timeout: parseFloat(timeout) * 1000,
|
|
112
89
|
};
|
|
113
90
|
}
|
|
114
|
-
static serverAliveWait(cluster, server, aliveTimeoutCorrection =
|
|
115
|
-
if (server
|
|
116
|
-
|
|
117
|
-
server.timer = undefined;
|
|
91
|
+
static serverAliveWait(cluster, server, message, aliveTimeoutCorrection, existingServer = true) {
|
|
92
|
+
if (!server) {
|
|
93
|
+
return;
|
|
118
94
|
}
|
|
119
|
-
|
|
120
|
-
if (timeout <= 0) {
|
|
95
|
+
if (server.timer === undefined && existingServer) {
|
|
121
96
|
return;
|
|
122
97
|
}
|
|
123
|
-
server.
|
|
98
|
+
clearTimeout(server.timer);
|
|
99
|
+
server.timer = undefined;
|
|
124
100
|
server.timestamp = Date.now();
|
|
101
|
+
server.timeout = message.timeout || 0;
|
|
125
102
|
server.timer = setTimeout(() => {
|
|
126
|
-
const
|
|
127
|
-
if (
|
|
103
|
+
const existing = cluster.find(server);
|
|
104
|
+
if (!existing) {
|
|
128
105
|
return;
|
|
129
106
|
}
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
107
|
+
const now = Date.now();
|
|
108
|
+
const delta = now - (existing.timestamp || now);
|
|
109
|
+
const currentTimeout = (existing.timeout || 0) +
|
|
110
|
+
aliveTimeoutCorrection;
|
|
111
|
+
if (delta >= currentTimeout) {
|
|
112
|
+
cluster.remove(server);
|
|
133
113
|
}
|
|
134
|
-
}, timeout);
|
|
114
|
+
}, server.timeout + aliveTimeoutCorrection);
|
|
135
115
|
}
|
|
136
116
|
/**
|
|
137
117
|
* Destroys the UDPClusterManager by closing all opened network connections
|
|
@@ -149,22 +129,21 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
149
129
|
}
|
|
150
130
|
return await new Promise((resolve, reject) => {
|
|
151
131
|
try {
|
|
152
|
-
if (typeof socket.close
|
|
153
|
-
|
|
154
|
-
socket.close(() => {
|
|
155
|
-
// unref may be missing or not a function on mocked sockets
|
|
156
|
-
if (socket && typeof socket.unref === 'function') {
|
|
157
|
-
socket.unref();
|
|
158
|
-
}
|
|
159
|
-
if (socketKey
|
|
160
|
-
&& UDPClusterManager.sockets[socketKey]) {
|
|
161
|
-
delete UDPClusterManager.sockets[socketKey];
|
|
162
|
-
}
|
|
163
|
-
resolve();
|
|
164
|
-
});
|
|
132
|
+
if (typeof socket.close !== 'function') {
|
|
133
|
+
resolve();
|
|
165
134
|
return;
|
|
166
135
|
}
|
|
167
|
-
|
|
136
|
+
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
|
+
});
|
|
168
147
|
}
|
|
169
148
|
catch (e) {
|
|
170
149
|
reject(e);
|
|
@@ -177,11 +156,7 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
177
156
|
const broadcastAddress = options.broadcastAddress
|
|
178
157
|
|| limitedBroadcastAddress;
|
|
179
158
|
const defaultAddress = '0.0.0.0';
|
|
180
|
-
if (!broadcastAddress) {
|
|
181
|
-
return defaultAddress;
|
|
182
|
-
}
|
|
183
|
-
const equalAddresses = broadcastAddress === limitedBroadcastAddress;
|
|
184
|
-
if (equalAddresses) {
|
|
159
|
+
if (!broadcastAddress || broadcastAddress === limitedBroadcastAddress) {
|
|
185
160
|
return defaultAddress;
|
|
186
161
|
}
|
|
187
162
|
for (const key in interfaces) {
|