forked from SocketCluster/scc-broker-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
227 lines (199 loc) · 6.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const socketClusterClient = require('socketcluster-client');
const ClusterBrokerClient = require('./cluster-broker-client');
const packageVersion = require('./package.json').version;
const DEFAULT_PORT = 7777;
const DEFAULT_RETRY_DELAY = 2000;
const DEFAULT_STATE_SERVER_CONNECT_TIMEOUT = 3000;
const DEFAULT_STATE_SERVER_ACK_TIMEOUT = 2000;
const DEFAULT_RECONNECT_RANDOMNESS = 1000;
module.exports.attach = function (broker, options) {
let reconnectRandomness = options.stateServerReconnectRandomness || DEFAULT_RECONNECT_RANDOMNESS;
let authKey = options.authKey || null;
let clusterClient = new ClusterBrokerClient(broker, {
authKey: authKey,
mappingEngine: options.mappingEngine,
clientPoolSize: options.clientPoolSize,
});
let retryDelay = options.brokerRetryDelay || DEFAULT_RETRY_DELAY;
let scStateSocketOptions = {
hostname: options.stateServerHost, // Required option
port: options.stateServerPort || DEFAULT_PORT,
connectTimeout: options.stateServerConnectTimeout || DEFAULT_STATE_SERVER_CONNECT_TIMEOUT,
ackTimeout: options.stateServerAckTimeout || DEFAULT_STATE_SERVER_ACK_TIMEOUT,
autoReconnectOptions: {
initialDelay: retryDelay,
randomness: reconnectRandomness,
multiplier: 1,
maxDelay: retryDelay + reconnectRandomness
},
query: {
authKey,
instancePort: options.instancePort,
instanceType: 'scc-worker',
version: packageVersion
}
};
let stateSocket = socketClusterClient.create(scStateSocketOptions);
(async () => {
for await (let event of stateSocket.listener('error')) {
clusterClient.emit('error', event);
}
})();
let latestBrokersSnapshotTime = -1;
let isNewBrokersSnapshot = (updatePacket) => {
if (updatePacket.time > latestBrokersSnapshotTime) {
latestBrokersSnapshotTime = updatePacket.time;
return true;
}
return false;
};
let resetBrokersSnapshotTime = () => {
latestBrokersSnapshotTime = -1;
};
let updateBrokerMapping = (req) => {
let data = req.data || {};
let updated = isNewBrokersSnapshot(data);
if (updated) {
clusterClient.setBrokers(data.sccBrokerURIs);
}
req.end();
};
let latestWorkersSnapshotTime = -1;
let isNewWorkersSnapshot = (updatePacket) => {
if (updatePacket.time > latestWorkersSnapshotTime) {
latestWorkersSnapshotTime = updatePacket.time;
return true;
}
return false;
};
let resetWorkersSnapshotTime = () => {
latestWorkersSnapshotTime = -1;
};
let triggerUpdateWorkers = (data) => {
clusterClient.emit('updateWorkers', {
workerURIs: data.sccWorkerURIs,
sourceWorkerURI: data.sccSourceWorkerURI
});
};
let updateWorkerMapping = (req) => {
let data = req.data || {};
let updated = isNewWorkersSnapshot(data);
if (updated) {
triggerUpdateWorkers(data);
}
req.end();
};
(async () => {
for await (let req of stateSocket.procedure('sccBrokerJoinCluster')) {
updateBrokerMapping(req);
}
})();
(async () => {
for await (let req of stateSocket.procedure('sccBrokerLeaveCluster')) {
updateBrokerMapping(req);
}
})();
(async () => {
for await (let req of stateSocket.procedure('sccWorkerJoinCluster')) {
updateWorkerMapping(req);
}
})();
(async () => {
for await (let req of stateSocket.procedure('sccWorkerLeaveCluster')) {
updateWorkerMapping(req);
}
})();
let sccWorkerStateData = {
instanceId: options.instanceId,
instanceIp: options.instanceIp,
instanceIpFamily: options.instanceIpFamily || 'IPv4'
};
let emitSCCWorkerJoinCluster = async () => {
let data;
try {
data = await stateSocket.invoke('sccWorkerJoinCluster', sccWorkerStateData);
} catch (error) {
stateSocket.emit('error', {error});
setTimeout(emitSCCWorkerJoinCluster, retryDelay);
return;
}
resetBrokersSnapshotTime();
resetWorkersSnapshotTime();
clusterClient.setBrokers(data.sccBrokerURIs);
triggerUpdateWorkers(data);
};
(async () => {
for await (let event of stateSocket.listener('connect')) {
emitSCCWorkerJoinCluster();
}
})();
let clusterMessageHandler = (channelName, packet) => {
if ((packet.sender == null || packet.sender !== options.instanceId) && packet.messages && packet.messages.length) {
packet.messages.forEach((data) => {
broker.invokePublish(channelName, data, true);
});
}
};
(async () => {
for await (let {channel, packet} of clusterClient.listener('message')) {
clusterMessageHandler(channel, packet);
}
})();
(async () => {
for await (let {channel} of broker.listener('subscribe')) {
if (clusterClient.isReady) {
clusterClient.subscribe(channel);
}
}
})();
(async () => {
for await (let {channel} of broker.listener('unsubscribe')) {
if (clusterClient.isReady) {
clusterClient.unsubscribe(channel);
}
}
})();
let publishOutboundBuffer = {};
let publishTimeout = null;
let flushPublishOutboundBuffer = () => {
Object.keys(publishOutboundBuffer).forEach(async (channelName) => {
let packet = {
sender: options.instanceId || null,
messages: publishOutboundBuffer[channelName],
};
try {
await clusterClient.invokePublish(channelName, packet);
} catch (error) {
clusterClient.emit('error', {error});
}
});
publishOutboundBuffer = {};
publishTimeout = null;
};
(async () => {
for await (let {channel, data} of broker.listener('publish')) {
if (options.pubSubBatchDuration == null) {
let packet = {
sender: options.instanceId || null,
messages: [data],
};
(async () => {
try {
await clusterClient.invokePublish(channel, packet);
} catch (error) {
clusterClient.emit('error', {error});
}
})();
} else {
if (!publishOutboundBuffer[channel]) {
publishOutboundBuffer[channel] = [];
}
publishOutboundBuffer[channel].push(data);
if (!publishTimeout) {
publishTimeout = setTimeout(flushPublishOutboundBuffer, options.pubSubBatchDuration);
}
}
}
})();
return clusterClient;
};