Skip to content

Commit 271db74

Browse files
authored
Expose inflightRange opt (#493)
* Add inflightRange opt * Fix: correct inflightRange-name in readme * Simplify * Pass inflightRange as arg to new Peer()
1 parent 9398c92 commit 271db74

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ Note that `tree`, `data`, and `bitfield` are normally heavily sparse files.
7474
encryptionKey: k, // optionally pass an encryption key to enable block encryption
7575
onwait: () => {}, // hook that is called if gets are waiting for download
7676
timeout: 0, // wait at max some milliseconds (0 means no timeout)
77-
writable: true // disable appends and truncates
77+
writable: true, // disable appends and truncates
78+
inflightRange: null // Advanced option. Set to [minInflight, maxInflight] to change the min and max inflight blocks per peer when downloading.
7879
}
7980
```
8081

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module.exports = class Hypercore extends EventEmitter {
6060
this.crypto = opts.crypto || hypercoreCrypto
6161
this.core = null
6262
this.replicator = null
63+
this.inflightRange = opts.inflightRange || null
6364
this.encryption = null
6465
this.extensions = new Map()
6566
this.cache = createCache(opts.cache)
@@ -404,6 +405,7 @@ module.exports = class Hypercore extends EventEmitter {
404405
eagerUpgrade: true,
405406
notDownloadingLinger: opts.notDownloadingLinger,
406407
allowFork: opts.allowFork !== false,
408+
inflightRange: this.inflightRange,
407409
onpeerupdate: this._onpeerupdate.bind(this),
408410
onupload: this._onupload.bind(this),
409411
oninvalid: this._oninvalid.bind(this)

lib/replicator.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,15 @@ class BlockTracker {
268268
}
269269

270270
class Peer {
271-
constructor (replicator, protomux, channel, session) {
271+
constructor (replicator, protomux, channel, session, inflightRange) {
272272
this.tracer = createTracer(this, { parent: replicator.core.tracer })
273273
this.core = replicator.core
274274
this.replicator = replicator
275275
this.stream = protomux.stream
276276
this.protomux = protomux
277277
this.remotePublicKey = this.stream.remotePublicKey
278278
this.remoteSupportsSeeks = false
279+
this.inflightRange = inflightRange
279280

280281
this.paused = false
281282

@@ -299,7 +300,6 @@ class Peer {
299300
this.receiverBusy = false
300301

301302
this.inflight = 0
302-
this.inflightRange = DEFAULT_MAX_INFLIGHT
303303
this.dataProcessing = 0
304304

305305
this.canUpgrade = true
@@ -1221,6 +1221,7 @@ module.exports = class Replicator {
12211221
notDownloadingLinger = NOT_DOWNLOADING_SLACK,
12221222
eagerUpgrade = true,
12231223
allowFork = true,
1224+
inflightRange = null,
12241225
onpeerupdate = noop,
12251226
onupload = noop,
12261227
oninvalid = noop
@@ -1241,6 +1242,8 @@ module.exports = class Replicator {
12411242
this.downloading = false
12421243
this.activeSessions = 0
12431244

1245+
this.inflightRange = inflightRange || DEFAULT_MAX_INFLIGHT
1246+
12441247
this._attached = new Set()
12451248
this._inflight = new InflightTracker()
12461249
this._blocks = new BlockTracker()
@@ -2090,7 +2093,7 @@ module.exports = class Replicator {
20902093

20912094
if (channel === null) return onnochannel()
20922095

2093-
const peer = new Peer(replicator, protomux, channel, session)
2096+
const peer = new Peer(replicator, protomux, channel, session, this.inflightRange)
20942097
const stream = protomux.stream
20952098

20962099
peer.channel.open({

test/replicate.js

+20
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,26 @@ test('replication updates on core copy', async function (t) {
14861486
await t.execution(promise)
14871487
})
14881488

1489+
test('can define default max-inflight blocks for replicator peers', async function (t) {
1490+
const a = new Hypercore(RAM, { inflightRange: [123, 123] })
1491+
await a.append('some block')
1492+
1493+
const b = await create(a.key)
1494+
replicate(a, b, t)
1495+
await b.get(0)
1496+
1497+
t.alike(
1498+
a.replicator.peers[0].inflightRange,
1499+
[123, 123],
1500+
'Uses the custom inflight range'
1501+
)
1502+
t.alike(
1503+
b.replicator.peers[0].inflightRange,
1504+
[32, 512],
1505+
'Uses default if no inflight range specified'
1506+
)
1507+
})
1508+
14891509
async function waitForRequestBlock (core) {
14901510
while (true) {
14911511
const reqBlock = core.replicator._inflight._requests.find(req => req && req.block)

0 commit comments

Comments
 (0)