Skip to content

Commit ed965bf

Browse files
authored
at max look at 64 ranges (random) on each tick (#594)
1 parent 7dcd252 commit ed965bf

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

lib/replicator.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const DEFAULT_SEGMENT_SIZE = 256 * 1024 * 8 // 256 KiB in bits
3838
const NOT_DOWNLOADING_SLACK = 20000 + (Math.random() * 20000) | 0
3939
const MAX_PEERS_UPGRADE = 3
4040

41+
const MAX_RANGES = 64
42+
4143
const PRIORITY = {
4244
NORMAL: 0,
4345
HIGH: 1,
@@ -1623,11 +1625,18 @@ module.exports = class Replicator {
16231625

16241626
const ref = r.attach(session)
16251627

1626-
this._ranges.push(r)
1627-
16281628
// Trigger this to see if this is already resolved...
16291629
// Also auto compresses the range based on local bitfield
1630-
this._updateNonPrimary(true)
1630+
clampRange(this.core, r)
1631+
1632+
this._ranges.push(r)
1633+
1634+
if (r.end !== -1 && r.start >= r.end) {
1635+
this._resolveRangeRequest(r, this._ranges.length - 1)
1636+
return ref
1637+
}
1638+
1639+
this.updateAll()
16311640

16321641
return ref
16331642
}
@@ -1899,13 +1908,16 @@ module.exports = class Replicator {
18991908
async _updateNonPrimary (updateAll) {
19001909
// Check if running, if so skip it and the running one will issue another update for us (debounce)
19011910
while (++this._updatesPending === 1) {
1902-
for (let i = 0; i < this._ranges.length; i++) {
1911+
let len = Math.min(MAX_RANGES, this._ranges.length)
1912+
1913+
for (let i = 0; i < len; i++) {
19031914
const r = this._ranges[i]
19041915

19051916
clampRange(this.core, r)
19061917

19071918
if (r.end !== -1 && r.start >= r.end) {
19081919
this._resolveRangeRequest(r, i--)
1920+
if (len > this._ranges.length) len--
19091921
}
19101922
}
19111923

@@ -2187,11 +2199,13 @@ module.exports = class Replicator {
21872199
}
21882200

21892201
const ranges = new RandomIterator(this._ranges)
2202+
let tried = 0
21902203

21912204
for (const r of ranges) {
21922205
if (peer._requestRange(r) === true) {
21932206
return true
21942207
}
2208+
if (++tried >= MAX_RANGES) break
21952209
}
21962210

21972211
// Iterate from newest fork to oldest fork...

0 commit comments

Comments
 (0)