forked from SocketCluster/scc-broker-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster-broker-client.js
215 lines (190 loc) · 7.52 KB
/
cluster-broker-client.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
const ClientPool = require('./client-pool');
const AsyncStreamEmitter = require('async-stream-emitter');
const SimpleMapper = require('./mappers/simple-mapper');
const SkeletonRendezvousMapper = require('./mappers/skeleton-rendezvous-mapper');
function ClusterBrokerClient(broker, options) {
AsyncStreamEmitter.call(this);
options = options || {};
this.broker = broker;
this.sccBrokerClientPools = {};
this.sccBrokerURIList = [];
this.authKey = options.authKey || null;
this.mappingEngine = options.mappingEngine || 'skeletonRendezvous';
this.clientPoolSize = options.clientPoolSize || 1;
this.isReady = false;
if (this.mappingEngine === 'skeletonRendezvous') {
this.mapper = new SkeletonRendezvousMapper(options.mappingEngineOptions);
} else if (this.mappingEngine === 'simple') {
this.mapper = new SimpleMapper(options.mappingEngineOptions);
} else {
if (typeof this.mappingEngine !== 'object') {
throw new Error(`The specified mappingEngine '${this.mappingEngine}' is not a valid engine - It must be either 'simple', 'skeletonRendezvous' or a custom mappingEngine instance`);
}
this.mapper = this.mappingEngine;
}
}
ClusterBrokerClient.prototype = Object.create(AsyncStreamEmitter.prototype);
ClusterBrokerClient.prototype.errors = {
NoMatchingSubscribeTargetError: function (channelName) {
let err = new Error(`Could not find a matching subscribe target scc-broker for the ${channelName} channel - The scc-broker may be down`);
err.name = 'NoMatchingSubscribeTargetError';
return err;
},
NoMatchingUnsubscribeTargetError: function (channelName) {
let err = new Error(`Could not find a matching unsubscribe target scc-broker for the ${channelName} channel - The scc-broker may be down`);
err.name = 'NoMatchingUnsubscribeTargetError';
return err;
},
NoMatchingPublishTargetError: function (channelName) {
let err = new Error(`Could not find a matching publish target scc-broker for the ${channelName} channel - The scc-broker may be down`);
err.name = 'NoMatchingPublishTargetError';
return err;
}
};
ClusterBrokerClient.prototype.mapChannelNameToBrokerURI = function (channelName) {
return this.mapper.findSite(channelName);
};
ClusterBrokerClient.prototype.setBrokers = function (sccBrokerURIList) {
this.sccBrokerURIList = sccBrokerURIList.concat();
this.mapper.setSites(this.sccBrokerURIList);
let brokerClientMap = {};
let fullSubscriptionList = this.getAllSubscriptions();
this.sccBrokerURIList.forEach((clientURI) => {
if (this.sccBrokerClientPools[clientURI]) {
brokerClientMap[clientURI] = this.sccBrokerClientPools[clientURI];
return;
}
let clientPool = new ClientPool({
clientCount: this.clientPoolSize,
targetURI: clientURI,
authKey: this.authKey
});
brokerClientMap[clientURI] = clientPool;
this.sccBrokerClientPools[clientURI] = clientPool;
(async () => {
for await (let event of clientPool.listener('error')) {
this.emit('error', event);
}
})();
(async () => {
for await (let event of clientPool.listener('subscribe')) {
this.emit('subscribe', event);
}
})();
(async () => {
for await (let event of clientPool.listener('subscribeFail')) {
this.emit('subscribeFail', event);
}
})();
(async () => {
for await (let event of clientPool.listener('publish')) {
this.emit('publish', event);
}
})();
(async () => {
for await (let event of clientPool.listener('publishFail')) {
this.emit('publishFail', event);
}
})();
});
let unusedsccBrokerURIList = Object.keys(this.sccBrokerClientPools).filter((clientURI) => {
return !brokerClientMap[clientURI];
});
unusedsccBrokerURIList.forEach((clientURI) => {
let unusedClientPool = this.sccBrokerClientPools[clientURI];
unusedClientPool.destroy();
delete this.sccBrokerClientPools[clientURI];
});
let newSubscriptionsMap = {};
fullSubscriptionList.forEach((channelName) => {
let targetsccBrokerURI = this.mapChannelNameToBrokerURI(channelName);
if (!newSubscriptionsMap[targetsccBrokerURI]) {
newSubscriptionsMap[targetsccBrokerURI] = {};
}
if (!newSubscriptionsMap[targetsccBrokerURI][channelName]) {
newSubscriptionsMap[targetsccBrokerURI][channelName] = true;
}
});
Object.keys(this.sccBrokerClientPools).forEach((clientURI) => {
let targetClientPool = this.sccBrokerClientPools[clientURI];
let newChannelLookup = newSubscriptionsMap[clientURI] || {};
let existingChannelList = targetClientPool.subscriptions(true);
existingChannelList.forEach((channelName) => {
if (!newChannelLookup[channelName]) {
targetClientPool.closeChannel(channelName);
}
});
let newChannelList = Object.keys(newChannelLookup);
newChannelList.forEach((channelName) => {
this._subscribeClientPoolToChannelAndWatch(targetClientPool, channelName);
});
});
this.emit('updateBrokers', {brokerURIs: sccBrokerURIList});
this.isReady = true;
};
ClusterBrokerClient.prototype.getAllSubscriptions = function () {
let channelLookup = {};
Object.keys(this.sccBrokerClientPools).forEach((clientURI) => {
let clientPool = this.sccBrokerClientPools[clientURI];
let subs = clientPool.subscriptions(true);
subs.forEach((channelName) => {
if (!channelLookup[channelName]) {
channelLookup[channelName] = true;
}
});
});
let localBrokerSubscriptions = this.broker.subscriptions();
localBrokerSubscriptions.forEach((channelName) => {
channelLookup[channelName] = true;
});
return Object.keys(channelLookup);
};
ClusterBrokerClient.prototype._handleChannelMessage = function (channelName, packet) {
this.emit('message', {
channel: channelName,
packet
});
};
ClusterBrokerClient.prototype._subscribeClientPoolToChannelAndWatch = function (clientPool, channelName) {
if (clientPool.isSubscribed(channelName, true)) {
return;
}
let channel = clientPool.subscribe(channelName);
(async () => {
for await (let data of channel) {
this._handleChannelMessage(channelName, data);
}
})();
};
ClusterBrokerClient.prototype.subscribe = function (channelName) {
let targetsccBrokerURI = this.mapChannelNameToBrokerURI(channelName);
let targetsccBrokerClientPool = this.sccBrokerClientPools[targetsccBrokerURI];
if (targetsccBrokerClientPool) {
this._subscribeClientPoolToChannelAndWatch(targetsccBrokerClientPool, channelName);
} else {
let error = this.errors.NoMatchingSubscribeTargetError(channelName);
this.emit('error', {error});
}
};
ClusterBrokerClient.prototype.unsubscribe = function (channelName) {
let targetsccBrokerURI = this.mapChannelNameToBrokerURI(channelName);
let targetsccBrokerClientPool = this.sccBrokerClientPools[targetsccBrokerURI];
if (targetsccBrokerClientPool) {
targetsccBrokerClientPool.unsubscribe(channelName);
targetsccBrokerClientPool.closeChannel(channelName);
} else {
let error = this.errors.NoMatchingUnsubscribeTargetError(channelName);
this.emit('error', {error});
}
};
ClusterBrokerClient.prototype.invokePublish = function (channelName, data) {
let targetsccBrokerURI = this.mapChannelNameToBrokerURI(channelName);
let targetsccBrokerClientPool = this.sccBrokerClientPools[targetsccBrokerURI];
if (targetsccBrokerClientPool) {
targetsccBrokerClientPool.invokePublish(channelName, data);
} else {
let error = this.errors.NoMatchingPublishTargetError(channelName);
this.emit('error', {error});
}
};
module.exports = ClusterBrokerClient;