Skip to content

Commit

Permalink
at max wait for 3 peers to resolve explicit upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Sep 27, 2024
1 parent a001bb8 commit fca27f0
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const DEFAULT_MAX_INFLIGHT = [16, 512]
const SCALE_LATENCY = 50
const DEFAULT_SEGMENT_SIZE = 256 * 1024 * 8 // 256 KiB in bits
const NOT_DOWNLOADING_SLACK = 20000 + (Math.random() * 20000) | 0
const MAX_PEERS_UPGRADE = 3

const PRIORITY = {
NORMAL: 0,
Expand Down Expand Up @@ -1667,12 +1668,15 @@ module.exports = class Replicator {
// instead its more efficient to only call it when the conditions in here change - ie on sync/add/remove peer
// Do this when we have more tests.
_checkUpgradeIfAvailable () {
if (this._ifAvailable > 0 || this._upgrade === null || this._upgrade.refs.length === 0) return
if (this._ifAvailable > 0 && this.peers.length < MAX_PEERS_UPGRADE) return
if (this._upgrade === null || this._upgrade.refs.length === 0) return
if (this._hadPeers === false && this.findingPeers > 0) return

const maxPeers = Math.min(this.peers.length, MAX_PEERS_UPGRADE)

// check if a peer can upgrade us

for (let i = 0; i < this.peers.length; i++) {
for (let i = 0; i < maxPeers; i++) {
const peer = this.peers[i]

if (peer.remoteSynced === false) return
Expand All @@ -1699,6 +1703,9 @@ module.exports = class Replicator {
if (r.inflight.length > 0) return
}

// if something is inflight, wait for that first
if (this._upgrade.inflight.length > 0) return

// nothing to do, indicate no update avail

const u = this._upgrade
Expand Down

0 comments on commit fca27f0

Please sign in to comment.