Skip to content

Commit

Permalink
Add peer.remoteContiguousLength
Browse files Browse the repository at this point in the history
  • Loading branch information
LuKks committed Jul 6, 2023
1 parent ef96761 commit 0c88df6
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ class Peer {
replicator._ifAvailable++
}

get remoteContiguousLength () {
return this.remoteBitfield.findFirst(false, 0)
}

getMaxInflight () {
const stream = this.stream.rawStream
if (!stream.udx) return Math.min(this.inflightRange[1], this.inflightRange[0] * 3)
Expand Down Expand Up @@ -680,8 +684,12 @@ class Peer {

onrange ({ drop, start, length }) {
const has = drop === false
const rangeStart = this.remoteBitfield.findFirst(!has, start)
const rangeLength = length - (rangeStart - start)

this.remoteBitfield.setRange(start, length, has)
if (rangeLength > 0) {
this.remoteBitfield.setRange(rangeStart, rangeLength, has)
}

if (drop === false) this._update()
}
Expand Down Expand Up @@ -912,6 +920,8 @@ class Peer {
}

_maybeWant (start, length = 1) {
if (start + length <= this.remoteContiguousLength) return

let i = Math.floor(start / DEFAULT_SEGMENT_SIZE)
const n = Math.ceil((start + length) / DEFAULT_SEGMENT_SIZE)

Expand Down
68 changes: 68 additions & 0 deletions test/remote-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const test = require('brittle')
const { create, replicate } = require('./helpers')

test('basic peer remote contiguous length', async function (t) {
t.plan(2)

const a = await create()
const b = await create(a.key)

replicate(a, b, t)

await a.append('a')
await b.get(0)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 1)

await a.append('b')
await b.get(1)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 2)
})

test('peer remote contiguous length', async function (t) {
t.plan(3)

const a = await create()
const b = await create(a.key)

replicate(a, b, t)

await a.append('a')
await b.get(0)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 1)

await a.append(['d', 'e'])
await b.get(2)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 1)

await b.get(1)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 3)
})

test('peer truncates the remote contiguous length', async function (t) {
t.plan(2)

const a = await create()
const b = await create(a.key)

replicate(a, b, t)

await a.append(['a', 'b'])
await b.get(0)
await b.get(1)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 2)

await a.truncate(1)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 1)
})

// "A" wants to know if "B" is finished syncing, so find the corresponding peer
function getPeer (a, b) {
return a.replicator.peers.find(peer => peer.remotePublicKey.equals(b.replicator.peers[0].stream.publicKey))
}

0 comments on commit 0c88df6

Please sign in to comment.