This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathpeer_heap.js
356 lines (287 loc) · 9.65 KB
/
peer_heap.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (c) 2020 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');
module.exports = PeerHeap;
// A max-score (pre-computed) for peer selection
// This is used for every DFS. It will end up being the size of the largest
// peer list.
var dfsStack = [0, 1, 2];
function PeerHeap(peers, random) {
this.array = [];
this.peers = peers || null;
this.hasMinConnections = peers ? peers.hasMinConnections : false;
this.random = random || Math.random;
assert(typeof this.random === 'function', 'PeerHeap expected random fn');
// We cache the range los and his so they can be stored as a contiguous
// array of boxed doubles. This has a noticable speed increase.
this.rangehis = [];
this.rangelos = [];
this.maxRangeStart = 0;
}
/*eslint-disable complexity */
/*eslint-disable max-statements */
PeerHeap.prototype.choose = function choose(threshold, filter) {
var self = this;
if (!self.array.length) {
return null;
}
var isSecondary = false;
var chosenPeer = null;
var secondaryPeer = null;
var highestProbability = 0;
var secondaryProbability = 0;
var firstScore = self.array[0].peer.getScore();
var notEnoughPeers = false;
if (self.hasMinConnections) {
notEnoughPeers = self.peers.currentConnectedPeers < self.peers.minConnections;
}
// Pointers into dfsStack
var stackBegin = 0;
var stackEnd = 0;
if (firstScore > threshold && (!filter || filter(self.array[0].peer))) {
// Don't check first peer if it looks good, check its children though
var firstPeer = self.array[0].peer;
isSecondary = notEnoughPeers && firstPeer.isConnected('out');
if (isSecondary) {
secondaryPeer = firstPeer;
secondaryProbability = firstScore;
} else {
chosenPeer = firstPeer;
highestProbability = firstScore;
}
// The array is seeded with 0, 1, 2 so we just have to advance the
// stack pointers
stackBegin = 1;
stackEnd = 2;
}
while (stackBegin <= stackEnd) {
var i = dfsStack[stackBegin];
stackBegin++;
var el = self.array[i];
if (!el || (
self.rangehis[i] <= self.maxRangeStart &&
(!filter && !notEnoughPeers)
)) {
// This range ends before the range with the largest start begins,
// so it can't possibly be chosen over any of the ranges we've
// seen. All ranges below this one have a smaller end, so this
// range and any below it can't be chosen.
continue;
}
if (!filter || filter(el.peer)) {
isSecondary = notEnoughPeers && el.peer.isConnected('out');
// INLINE of TChannelPeer#getScore
var lo = self.rangelos[i];
var hi = self.rangehis[i];
var rand = self.random();
if (rand === 0) {
rand = 1;
}
var probability = lo + ((hi - lo) * rand);
var isBestChoice = !isSecondary ?
(probability > highestProbability) :
(probability > secondaryProbability);
if ((probability > threshold) && isBestChoice) {
if (isSecondary) {
secondaryProbability = probability;
secondaryPeer = el.peer;
} else {
highestProbability = probability;
chosenPeer = el.peer;
}
}
}
// Continue DFS by 'pushing' left and right indexes onto end of
// dfsStack, if the source array is long enough for that
var left = 2 * i + 1;
var right = left + 1;
if (left < self.array.length) {
dfsStack[++stackEnd] = left;
if (right < self.array.length) {
dfsStack[++stackEnd] = right;
}
}
}
if (secondaryProbability > highestProbability && chosenPeer) {
chosenPeer.tryConnect(noop);
return secondaryPeer;
}
return chosenPeer || secondaryPeer;
};
/*eslint-enable complexity */
/*eslint-enable max-statements */
function noop() {}
PeerHeap.prototype.clear = function clear() {
var self = this;
for (var i = 0; i < self.array.length; i++) {
var el = self.array[i];
el.heap = null;
el.peer = null;
el.index = 0;
el.range = null;
}
self.array.length = 0;
self.rangehis.length = 0;
self.rangelos.length = 0;
};
PeerHeap.prototype.add = function add(peer) {
var self = this;
var range = peer.scoreRange;
var i = self.push(peer, range);
var el = self.array[i];
return el;
};
PeerHeap.prototype.rescore = function rescore() {
var self = this;
for (var i = 0; i < self.array.length; i++) {
var el = self.array[i];
el.range = el.peer.getScoreRange();
}
self.heapify();
};
PeerHeap.prototype.heapify = function heapify() {
var self = this;
if (self.array.length <= 1) {
return;
}
for (var i = Math.floor(self.array.length / 2 - 1); i >= 0; i--) {
self.siftdown(i);
}
};
PeerHeap.prototype.remove = function remove(i) {
var self = this;
if (i >= self.array.length) {
return;
}
if (self.array.length === 1) {
self.array.pop();
return;
}
var j = self.array.length - 1;
if (i === j) {
self.array.pop();
return;
}
self.swap(i, j);
self.array.pop();
self.siftup(i);
};
PeerHeap.prototype.push = function push(peer, range) {
var self = this;
var el = new PeerHeapElement(self);
el.peer = peer;
el.range = peer.scoreRange;
el.index = self.array.length;
self.array.push(el);
return self.siftup(el.index);
};
PeerHeap.prototype.pop = function pop() {
var self = this;
var peer = null;
if (!self.array.length) {
return peer;
}
if (self.array.length === 1) {
peer = self.array.pop();
return peer;
}
peer = self.array[0].peer;
self.array[0] = self.array.pop();
self.siftdown(0);
return peer;
};
PeerHeap.prototype.siftdown = function siftdown(i) {
var self = this;
for (;;) {
var left = (2 * i) + 1;
if (left >= self.array.length) {
self.rangehis[i] = self.array[i].range.hi;
self.rangelos[i] = self.array[i].range.lo;
self.maxRangeStart = Math.max(self.rangelos[i], self.maxRangeStart);
return i;
}
var right = left + 1;
var child = left;
if (right < self.array.length &&
self.array[right].range.hi > self.array[left].range.hi) {
child = right;
}
if (self.array[child].range.hi > self.array[i].range.hi) {
self.swap(i, child);
i = child;
} else {
self.rangehis[i] = self.array[i].range.hi;
self.rangelos[i] = self.array[i].range.lo;
self.maxRangeStart = Math.max(self.rangelos[i], self.maxRangeStart);
return i;
}
}
};
PeerHeap.prototype.siftup = function siftup(i) {
var self = this;
while (i > 0) {
var par = Math.floor((i - 1) / 2);
if (self.array[i].range.hi > self.array[par].range.hi) {
self.swap(i, par);
i = par;
} else {
self.rangehis[i] = self.array[i].range.hi;
self.rangelos[i] = self.array[i].range.lo;
self.maxRangeStart = Math.max(self.rangelos[i], self.maxRangeStart);
return i;
}
}
self.rangehis[0] = self.array[0].range.hi;
self.rangelos[0] = self.array[0].range.lo;
self.maxRangeStart = Math.max(self.rangelos[i], self.maxRangeStart);
return 0;
};
PeerHeap.prototype.swap = function swap(i, j) {
var self = this;
var a = self.array[i];
var b = self.array[j];
self.array[i] = b;
self.array[j] = a;
b.index = i;
a.index = j;
self.rangehis[j] = a.range.hi;
self.rangelos[j] = a.range.lo;
self.maxRangeStart = Math.max(self.rangelos[j], self.maxRangeStart);
self.rangehis[i] = b.range.hi;
self.rangelos[i] = b.range.lo;
self.maxRangeStart = Math.max(self.rangelos[i], self.maxRangeStart);
};
function PeerHeapElement(heap) {
var self = this;
self.heap = heap;
self.peer = null;
self.index = 0;
self.range = null;
}
PeerHeapElement.prototype.rescore = function rescore(range) {
var self = this;
if (!self.heap) {
return;
}
self.range = self.peer.scoreRange;
self.index = self.heap.siftup(self.index);
self.index = self.heap.siftdown(self.index);
};