Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hotswaps #597

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,6 @@ module.exports = class Hypercore extends EventEmitter {
if (this.opened === false) await this.opening

const activeRequests = (range && range.activeRequests) || this.activeRequests

return this.replicator.addRange(activeRequests, range)
}

Expand Down
38 changes: 38 additions & 0 deletions lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ class Peer {
return Math.max(this.inflightRange[0], Math.round(Math.min(this.inflightRange[1], this.inflightRange[0] * scale)))
}

getMaxHotswapInflight () {
const inf = this.getMaxInflight()
return Math.max(16, inf / 2)
}

signalUpgrade () {
if (this._shouldUpdateCanUpgrade() === true) this._updateCanUpgradeAndSync()
else this.sendSync()
Expand Down Expand Up @@ -1838,6 +1843,13 @@ module.exports = class Replicator {

b.resolve(value)

if (b.inflight.length > 0) {
for (let i = b.inflight.length - 1; i >= 0; i--) {
const req = b.inflight[i]
req.peer._cancelRequest(req)
}
}

return true
}

Expand Down Expand Up @@ -2161,6 +2173,28 @@ module.exports = class Replicator {
return false
}

_updateHotswap (peer) {
const maxHotswaps = peer.getMaxHotswapInflight()
if (!peer.isActive() || peer.inflight >= maxHotswaps) return

console.log('in update hotswap!')

let n = 0

for (const b of this._blocks) {
if (b.inflight.length !== 1) continue
if (b.inflight[0].peer === peer) continue

if (peer._requestBlock(b) === false) continue

n++

if (peer.inflight >= maxHotswaps) break
}

console.log('asked for', n, 'swaps')
}

_updatePeer (peer) {
if (!peer.isActive() || peer.inflight >= peer.getMaxInflight()) {
return false
Expand Down Expand Up @@ -2230,6 +2264,10 @@ module.exports = class Replicator {
while (this._updatePeer(peer) === true);
while (this._updatePeerNonPrimary(peer) === true);

if (this.peers.length > 1 && this._blocks.isEmpty() === false) {
this._updateHotswap(peer)
}

this._checkUpgradeIfAvailable()
this._maybeResolveIfAvailableRanges()
}
Expand Down
Loading