| 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 |
1
1
1
1
1
1
1
472
236
236
236
236
236
236
236
236
236
1
1
223
223
223
223
379
223
1
379
223
223
1
15
15
1
1001
1001
1001
163
3
160
22
138
138
160
998
1
40
40
40
40
40
1
230
230
1
1576
1576
1576
1576
1509
1576
1
1
225
225
225
225
124
124
98
98
225
225
1
3
3
3
2
2
2
2
3
3
1
1
666
666
666
666
1
795
795
795
795
12
783
783
776
776
776
776
776
832
832
832
803
803
803
769
769
776
1
| // Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var assert = require('assert');
var inherits = require('util').inherits;
var EventEmitter = require('./lib/event_emitter');
var errors = require('./errors');
var TChannelPeer = require('./peer');
var TChannelSelfPeer = require('./self_peer');
function TChannelPeers(channel, options) {
if (!(this instanceof TChannelPeers)) {
return new TChannelPeers(channel, options);
}
var self = this;
EventEmitter.call(self);
self.allocPeerEvent = self.defineEvent('allocPeer');
self.channel = channel;
self.logger = self.channel.logger;
self.options = options || {};
self._map = Object.create(null);
self.selfPeer = TChannelSelfPeer(self.channel);
}
inherits(TChannelPeers, EventEmitter);
TChannelPeers.prototype.close = function close(callback) {
var self = this;
var peers = [self.selfPeer].concat(self.values());
var counter = peers.length;
peers.forEach(function eachPeer(peer) {
peer.close(onClose);
});
self.clear();
function onClose() {
if (--counter <= 0) {
Iif (counter < 0) {
self.logger.error('closed more peers than expected', {
counter: counter
});
}
callback();
}
}
};
TChannelPeers.prototype.get = function get(hostPort) {
var self = this;
return self._map[hostPort] || null;
};
TChannelPeers.prototype.add = function add(hostPort, options) {
var self = this;
var peer = self._map[hostPort];
if (!peer) {
if (hostPort === self.channel.hostPort) {
return self.selfPeer;
}
if (self.channel.topChannel) {
peer = self.channel.topChannel.peers.add(hostPort);
} else {
peer = TChannelPeer(self.channel, hostPort, options);
self.allocPeerEvent.emit(self, peer);
}
self._map[hostPort] = peer;
}
return peer;
};
TChannelPeers.prototype.addPeer = function addPeer(peer) {
var self = this;
assert(peer instanceof TChannelPeer, 'invalid peer');
assert(!self._map[peer.hostPort], 'peer already defined');
Eif (peer.hostPort !== self.channel.hostPort) {
self._map[peer.hostPort] = peer;
}
};
TChannelPeers.prototype.keys = function keys() {
var self = this;
return Object.keys(self._map);
};
TChannelPeers.prototype.values = function values() {
var self = this;
var keys = Object.keys(self._map);
var ret = new Array(keys.length);
for (var i = 0; i < keys.length; i++) {
ret[i] = self._map[keys[i]];
}
return ret;
};
TChannelPeers.prototype.entries = function entries() {
var self = this;
var keys = Object.keys(self._map);
var ret = new Array(keys.length);
for (var i = 0; i < keys.length; i++) {
ret[i] = [keys[i], self._map[keys[i]]];
}
return ret;
};
TChannelPeers.prototype.clear = function clear() {
var self = this;
var keys = self.keys();
var vals = new Array(keys.length);
if (self.channel.subChannels) {
var names = Object.keys(self.channel.subChannels);
for (var i = 0; i < names.length; i++) {
var subChannel = self.channel.subChannels[names[i]];
subChannel.peers._map = Object.create(null);
}
}
self._map = Object.create(null);
return vals;
};
TChannelPeers.prototype.delete = function del(hostPort) {
var self = this;
var peer = self._map[hostPort];
if (self.channel.subChannels) {
var names = Object.keys(self.channel.subChannels);
for (var i = 0; i < names.length; i++) {
var subChannel = self.channel.subChannels[names[i]];
delete subChannel.peers._map[hostPort];
}
}
delete self._map[hostPort];
return peer;
};
TChannelPeers.prototype.waitForIdentified =
function waitForIdentified(options, callback) {
var self = this;
assert(typeof options.host === 'string', 'options.host is required');
var peer = self.add(options.host);
peer.waitForIdentified(callback);
};
TChannelPeers.prototype.request = function peersRequest(req, options) {
var self = this;
var peer = self.choosePeer(req, options);
Iif (!peer) throw errors.NoPeerAvailable(); // TODO: operational error?
return peer.request(options);
};
TChannelPeers.prototype.choosePeer = function choosePeer(req, options) {
var self = this;
Iif (!options) options = {};
var hosts = null;
if (options.host) {
return self.add(options.host);
} else {
hosts = Object.keys(self._map);
}
if (!hosts || !hosts.length) return null;
var threshold = options.peerScoreThreshold;
Eif (threshold === undefined) threshold = self.options.peerScoreThreshold;
Eif (threshold === undefined) threshold = 0;
var selectedPeer = null, selectedScore = 0;
for (var i = 0; i < hosts.length; i++) {
var hostPort = hosts[i];
var peer = self.add(hostPort);
if (!req || !req.triedRemoteAddrs[hostPort]) {
var score = peer.state.shouldRequest(req, options);
var want = score > threshold &&
(selectedPeer === null || score > selectedScore);
if (want) {
selectedPeer = peer;
selectedScore = score;
}
}
}
return selectedPeer;
};
module.exports = TChannelPeers;
|