Commit dbb6b40 1 parent 405a2ce commit dbb6b40 Copy full SHA for dbb6b40
File tree 1 file changed +22
-3
lines changed
1 file changed +22
-3
lines changed Original file line number Diff line number Diff line change @@ -25,9 +25,23 @@ const kWeight = Symbol('kWeight')
25
25
const kMaxWeightPerServer = Symbol ( 'kMaxWeightPerServer' )
26
26
const kErrorPenalty = Symbol ( 'kErrorPenalty' )
27
27
28
+ /**
29
+ * Calculate the greatest common divisor of two numbers by
30
+ * using the Euclidean algorithm.
31
+ *
32
+ * @param {number } a
33
+ * @param {number } b
34
+ * @returns {number }
35
+ */
28
36
function getGreatestCommonDivisor ( a , b ) {
29
- if ( b === 0 ) return a
30
- return getGreatestCommonDivisor ( b , a % b )
37
+ if ( a === 0 ) return b
38
+
39
+ while ( b !== 0 ) {
40
+ const t = b
41
+ b = a % b
42
+ a = t
43
+ }
44
+ return a
31
45
}
32
46
33
47
function defaultFactory ( origin , opts ) {
@@ -105,7 +119,12 @@ class BalancedPool extends PoolBase {
105
119
}
106
120
107
121
_updateBalancedPoolStats ( ) {
108
- this [ kGreatestCommonDivisor ] = this [ kClients ] . map ( p => p [ kWeight ] ) . reduce ( getGreatestCommonDivisor , 0 )
122
+ let result = 0
123
+ for ( let i = 0 ; i < this [ kClients ] . length ; i ++ ) {
124
+ result = getGreatestCommonDivisor ( this [ kClients ] [ i ] [ kWeight ] , result )
125
+ }
126
+
127
+ this [ kGreatestCommonDivisor ] = result
109
128
}
110
129
111
130
removeUpstream ( upstream ) {
You can’t perform that action at this time.
0 commit comments