forked from SocketCluster/scc-broker-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-pool.js
172 lines (151 loc) · 4.8 KB
/
client-pool.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
const url = require('url');
const socketClusterClient = require('socketcluster-client');
const AsyncStreamEmitter = require('async-stream-emitter');
const Hasher = require('./hasher');
const trailingPortNumberRegex = /:[0-9]+$/;
function ClientPool(options) {
AsyncStreamEmitter.call(this);
options = options || {};
this.hasher = new Hasher();
this.clientCount = options.clientCount || 1;
this.targetURI = options.targetURI;
this.authKey = options.authKey;
let clientConnectOptions = this.breakDownURI(this.targetURI);
clientConnectOptions.query = {
authKey: this.authKey
};
clientConnectOptions.batchOnHandshake = true
clientConnectOptions.batchOnHandshakeDuration = 500
this._handleClientError = (event) => {
this.emit('error', event);
};
this._handleClientSubscribe = (client, channelName) => {
this.emit('subscribe', {
targetURI: this.targetURI,
poolIndex: client.poolIndex,
channel: channelName
});
};
this._handleClientSubscribeFail = (client, error, channelName) => {
this.emit('subscribeFail', {
targetURI: this.targetURI,
poolIndex: client.poolIndex,
error,
channel: channelName
});
};
this.clients = [];
for (let i = 0; i < this.clientCount; i++) {
let connectOptions = Object.assign({}, clientConnectOptions);
connectOptions.query.poolIndex = i;
let client = socketClusterClient.create(connectOptions);
client.poolIndex = i;
(async () => {
for await (let event of client.listener('error')) {
this._handleClientError(event);
}
})();
this.clients.push(client);
}
this._bindClientListeners();
}
ClientPool.prototype = Object.create(AsyncStreamEmitter.prototype);
ClientPool.prototype._bindClientListeners = function () {
this.clients.forEach((client) => {
(async () => {
for await (let event of client.listener('error')) {
this._handleClientError(event);
}
})();
(async () => {
for await (let {channel} of client.listener('subscribe')) {
this._handleClientSubscribe(client, channel);
}
})();
(async () => {
for await (let {error, channel} of client.listener('subscribeFail')) {
this._handleClientSubscribeFail(client, error, channel);
}
})();
});
};
ClientPool.prototype._unbindClientListeners = function () {
this.clients.forEach((client) => {
client.closeListener('error');
client.closeListener('subscribe');
client.closeListener('subscribeFail');
});
};
ClientPool.prototype.breakDownURI = function (uri) {
let parsedURI = url.parse(uri);
let hostname = parsedURI.host.replace(trailingPortNumberRegex, '');
let result = {
hostname: hostname,
port: parsedURI.port
};
if (parsedURI.protocol === 'wss:' || parsedURI.protocol === 'https:') {
result.secure = true;
}
return result;
};
ClientPool.prototype.selectClient = function (key) {
let targetIndex = this.hasher.hashToIndex(key, this.clients.length);
return this.clients[targetIndex];
};
ClientPool.prototype.invokePublish = async function (channelName, data) {
let targetClient = this.selectClient(channelName);
try {
await targetClient.invokePublish(channelName, data);
} catch (error) {
this.emit('publishFail', {
targetURI: this.targetURI,
poolIndex: targetClient.poolIndex,
channel: channelName,
error
});
return;
}
this.emit('publish', {
targetURI: this.targetURI,
poolIndex: targetClient.poolIndex,
channel: channelName,
data
});
};
ClientPool.prototype.subscriptions = function (includePending) {
let subscriptionList = [];
this.clients.forEach((client) => {
let clientSubList = client.subscriptions(includePending);
clientSubList.forEach((subscription) => {
subscriptionList.push(subscription);
});
});
return subscriptionList;
};
ClientPool.prototype.isSubscribed = function (channelName, includePending) {
let targetClient = this.selectClient(channelName);
return targetClient.isSubscribed(channelName, includePending);
};
ClientPool.prototype.unsubscribe = function (channelName) {
let targetClient = this.selectClient(channelName);
return targetClient.unsubscribe(channelName);
};
ClientPool.prototype.subscribe = function (channelName) {
let targetClient = this.selectClient(channelName);
return targetClient.subscribe(channelName);
};
ClientPool.prototype.closeChannel = function (channelName) {
let targetClient = this.selectClient(channelName);
targetClient.closeChannel(channelName);
};
ClientPool.prototype.destroy = function () {
this.clients.forEach((client) => {
client.disconnect();
client.subscriptions(true).forEach((channelName) => {
client.unsubscribe(channelName);
client.closeChannel(channelName);
});
});
this._unbindClientListeners();
};
module.exports = ClientPool;