@imqueue/core 2.0.6 → 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/README.md +0 -4
- package/debug.d.ts +1 -0
- package/debug_getter.d.ts +9 -0
- package/debug_listener.d.ts +1 -0
- package/package.json +1 -1
- package/src/ClusterManager.d.ts +2 -2
- package/src/ClusteredRedisQueue.d.ts +6 -2
- package/src/ClusteredRedisQueue.js +5 -7
- package/src/RedisQueue.js +3 -0
- package/src/UDPClusterManager.d.ts +4 -20
- package/src/UDPClusterManager.js +47 -86
package/README.md
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
# I Message Queue (@imqueue/core)
|
|
2
2
|
|
|
3
|
-
[](https://github.com/imqueue/core)
|
|
4
|
-
[](https://codebeat.co/projects/github-com-imqueue-core-master)
|
|
5
|
-
[](https://coveralls.io/github/imqueue/core?branch=master)
|
|
6
|
-
[](https://snyk.io/test/github/imqueue/core?targetFile=package.json)
|
|
7
3
|
[](https://rawgit.com/imqueue/core/master/LICENSE)
|
|
8
4
|
|
|
9
5
|
Simple JSON-based messaging queue for inter service communication
|
package/debug.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
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
|
}
|
package/src/RedisQueue.js
CHANGED
|
@@ -648,6 +648,9 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
648
648
|
}
|
|
649
649
|
const rx = new RegExp(`\\bname=${this.options.prefix}:[\\S]+?:watcher:`);
|
|
650
650
|
const list = await this.writer.client('LIST');
|
|
651
|
+
if (!list || !list.split) {
|
|
652
|
+
return 0;
|
|
653
|
+
}
|
|
651
654
|
return list.split(/\r?\n/).filter(client => rx.test(client)).length;
|
|
652
655
|
}
|
|
653
656
|
/**
|
|
@@ -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
|
@@ -14,11 +14,6 @@ exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = {
|
|
|
14
14
|
broadcastAddress: '255.255.255.255',
|
|
15
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,41 +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;
|
|
118
|
-
}
|
|
119
|
-
server.timestamp = Date.now();
|
|
120
|
-
if (message) {
|
|
121
|
-
server.timeout = message.timeout;
|
|
91
|
+
static serverAliveWait(cluster, server, message, aliveTimeoutCorrection, existingServer = true) {
|
|
92
|
+
if (!server) {
|
|
93
|
+
return;
|
|
122
94
|
}
|
|
123
|
-
|
|
124
|
-
const timeout = (server.timeout || 0) + correction;
|
|
125
|
-
if (timeout <= 0) {
|
|
95
|
+
if (server.timer === undefined && existingServer) {
|
|
126
96
|
return;
|
|
127
97
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
98
|
+
clearTimeout(server.timer);
|
|
99
|
+
server.timer = undefined;
|
|
100
|
+
server.timestamp = Date.now();
|
|
101
|
+
server.timeout = message.timeout || 0;
|
|
102
|
+
server.timer = setTimeout(() => {
|
|
103
|
+
const existing = cluster.find(server);
|
|
104
|
+
if (!existing) {
|
|
131
105
|
return;
|
|
132
106
|
}
|
|
133
107
|
const now = Date.now();
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
cluster.remove(existing);
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
const delta = now - existing.timestamp;
|
|
141
|
-
const currentTimeout = (existing.timeout || 0) + correction;
|
|
108
|
+
const delta = now - (existing.timestamp || now);
|
|
109
|
+
const currentTimeout = (existing.timeout || 0) +
|
|
110
|
+
aliveTimeoutCorrection;
|
|
142
111
|
if (delta >= currentTimeout) {
|
|
143
|
-
|
|
144
|
-
existing.timer = undefined;
|
|
145
|
-
cluster.remove(existing);
|
|
112
|
+
cluster.remove(server);
|
|
146
113
|
}
|
|
147
|
-
}, timeout);
|
|
148
|
-
server.timer = timerId;
|
|
114
|
+
}, server.timeout + aliveTimeoutCorrection);
|
|
149
115
|
}
|
|
150
116
|
/**
|
|
151
117
|
* Destroys the UDPClusterManager by closing all opened network connections
|
|
@@ -163,22 +129,21 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
163
129
|
}
|
|
164
130
|
return await new Promise((resolve, reject) => {
|
|
165
131
|
try {
|
|
166
|
-
if (typeof socket.close
|
|
167
|
-
|
|
168
|
-
socket.close(() => {
|
|
169
|
-
// unref may be missing or not a function on mocked sockets
|
|
170
|
-
if (socket && typeof socket.unref === 'function') {
|
|
171
|
-
socket.unref();
|
|
172
|
-
}
|
|
173
|
-
if (socketKey
|
|
174
|
-
&& UDPClusterManager.sockets[socketKey]) {
|
|
175
|
-
delete UDPClusterManager.sockets[socketKey];
|
|
176
|
-
}
|
|
177
|
-
resolve();
|
|
178
|
-
});
|
|
132
|
+
if (typeof socket.close !== 'function') {
|
|
133
|
+
resolve();
|
|
179
134
|
return;
|
|
180
135
|
}
|
|
181
|
-
|
|
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
|
+
});
|
|
182
147
|
}
|
|
183
148
|
catch (e) {
|
|
184
149
|
reject(e);
|
|
@@ -191,11 +156,7 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
191
156
|
const broadcastAddress = options.broadcastAddress
|
|
192
157
|
|| limitedBroadcastAddress;
|
|
193
158
|
const defaultAddress = '0.0.0.0';
|
|
194
|
-
if (!broadcastAddress) {
|
|
195
|
-
return defaultAddress;
|
|
196
|
-
}
|
|
197
|
-
const equalAddresses = broadcastAddress === limitedBroadcastAddress;
|
|
198
|
-
if (equalAddresses) {
|
|
159
|
+
if (!broadcastAddress || broadcastAddress === limitedBroadcastAddress) {
|
|
199
160
|
return defaultAddress;
|
|
200
161
|
}
|
|
201
162
|
for (const key in interfaces) {
|