Skip to content

Commit 8ee510c

Browse files
committed
unset inflight blocks
1 parent 533449a commit 8ee510c

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

lib/replicator.js

+40-14
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ class Peer {
354354
else this.sendSync()
355355
}
356356

357+
_markInflight (index) {
358+
this.missingBlocks.set(index, false)
359+
}
360+
357361
broadcastRange (start, length, drop) {
358362
if (drop) this._unclearLocalRange(start, length)
359363
else this._clearLocalRange(start, length)
@@ -638,11 +642,12 @@ class Peer {
638642
}
639643

640644
_cancelRequest (id) {
641-
const exists = this.replicator._inflight.get(id)
642-
if (!exists) return
645+
const req = this.replicator._inflight.get(id)
646+
if (!req) return
643647

644648
this.inflight--
645649
this.replicator._removeInflight(id)
650+
if (isBlockRequest(req)) this.replicator._unmarkInflight(req.block.index)
646651

647652
this.wireCancel.send({ request: id })
648653
}
@@ -692,6 +697,8 @@ class Peer {
692697
if (reorg === true) return await this.replicator._onreorgdata(this, req, data)
693698
} catch (err) {
694699
safetyCatch(err)
700+
if (isBlockRequest(req)) this.replicator._unmarkInflight(req.block.index)
701+
695702
this.paused = true
696703
this.replicator.oninvalid(err, req, data, this)
697704
return
@@ -706,6 +713,7 @@ class Peer {
706713
}
707714
} catch (err) {
708715
safetyCatch(err)
716+
if (isBlockRequest(req)) this.replicator._unmarkInflight(req.block.index)
709717

710718
if (err.code === 'WRITE_FAILED') {
711719
// For example, we don't want to keep pulling data when storage is full
@@ -795,9 +803,13 @@ class Peer {
795803
}
796804
}
797805

806+
_resetMissingBlock (index) {
807+
this.missingBlocks.set(index, this._remoteHasBlock(index) && !this.core.bitfield.get(index))
808+
}
809+
798810
_unclearLocalRange (start, length) {
799811
if (length === 1) {
800-
this.missingBlocks.set(start, this._remoteHasBlock(start) && !this.core.bitfield.get(start))
812+
this._resetMissingBlock(start)
801813
return
802814
}
803815

@@ -1010,6 +1022,14 @@ class Peer {
10101022
return index < this._remoteContiguousLength || this.remoteBitfield.get(index) === true
10111023
}
10121024

1025+
_sendBlockRequest (req, b) {
1026+
req.block = { index: b.index, nodes: 0 }
1027+
this.replicator._markInflight(b.index)
1028+
1029+
b.inflight.push(req)
1030+
this._send(req)
1031+
}
1032+
10131033
_requestBlock (b) {
10141034
const { length, fork } = this.core.tree
10151035

@@ -1025,10 +1045,7 @@ class Peer {
10251045
const req = this._makeRequest(b.index >= length, b.priority)
10261046
if (req === null) return false
10271047

1028-
req.block = { index: b.index, nodes: 0 }
1029-
1030-
b.inflight.push(req)
1031-
this._send(req)
1048+
this._sendBlockRequest(req, b)
10321049

10331050
return true
10341051
}
@@ -1047,10 +1064,7 @@ class Peer {
10471064
return false
10481065
}
10491066

1050-
req.block = { index, nodes: 0 }
1051-
1052-
b.inflight.push(req)
1053-
this._send(req)
1067+
this._sendBlockRequest(req, b)
10541068

10551069
// Don't think this will ever happen, as the pending queue is drained before the range queue
10561070
// but doesn't hurt to check this explicitly here also.
@@ -1552,11 +1566,14 @@ module.exports = class Replicator {
15521566
this.onpeerupdate(true, peer)
15531567
}
15541568

1569+
_markInflight (index) {
1570+
for (const peer of this.peers) peer._markInflight(index)
1571+
}
1572+
15551573
_removeInflight (id) {
15561574
this._inflight.remove(id)
1557-
if (this.isDownloading() === false) {
1558-
for (const peer of this.peers) peer.signalUpgrade()
1559-
}
1575+
if (this.isDownloading() === true) return
1576+
for (const peer of this.peers) peer.signalUpgrade()
15601577
}
15611578

15621579
_removePeer (peer) {
@@ -1759,6 +1776,7 @@ module.exports = class Replicator {
17591776
_clearRequest (peer, req) {
17601777
if (req.block !== null) {
17611778
this._clearInflightBlock(this._blocks, req)
1779+
this._unmarkInflight(req.block.index)
17621780
}
17631781

17641782
if (req.hash !== null) {
@@ -1783,6 +1801,10 @@ module.exports = class Replicator {
17831801
this.updateAll()
17841802
}
17851803

1804+
_unmarkInflight (index) {
1805+
for (const peer of this.peers) peer._resetMissingBlock(index)
1806+
}
1807+
17861808
_ondata (peer, req, data) {
17871809
if (data.block !== null) {
17881810
this._resolveBlockRequest(this._blocks, data.block.index, data.block.value, req)
@@ -2218,3 +2240,7 @@ function onwireextension (m, c) {
22182240
function setDownloadingLater (repl, downloading, session) {
22192241
repl.setDownloadingNow(downloading, session)
22202242
}
2243+
2244+
function isBlockRequest (req) {
2245+
return req !== null && req.block !== null
2246+
}

0 commit comments

Comments
 (0)