hyperdrive
Advanced tools
Comparing version 1.4.1 to 1.4.2
@@ -74,9 +74,3 @@ var thunky = require('thunky') | ||
if (this._state.blocks && index >= this._state.blocks) return cb(null, null) | ||
if (this._state.bitfield.get(index)) { | ||
this._state.get(index, cb) | ||
} else { | ||
this._state.want.push({block: index, callback: cb}) | ||
this._subswarm.fetch() | ||
} | ||
this._state.get(index, cb) | ||
} | ||
@@ -83,0 +77,0 @@ |
var protocol = require('./protocol') | ||
var debug = require('debug')('hyperdrive-swarm') | ||
var MAX_INFLIGHT_PER_PEER = 20 | ||
module.exports = Swarm | ||
@@ -9,3 +11,2 @@ | ||
if (!opts) opts = {} | ||
this.name = opts.name || 'unknown' | ||
this.prioritized = 0 | ||
@@ -16,4 +17,17 @@ this.drive = drive | ||
this.joined = {} | ||
this.kicking = false | ||
} | ||
Swarm.prototype._kick = function () { | ||
if (this.kicking) return | ||
this.kicking = true | ||
debug('polling all peers to see if they have something to do') | ||
// TODO: optimize this process - will iterate too much atm | ||
var ids = Object.keys(this.joined) | ||
for (var i = 0; i < ids.length; i++) { | ||
this.joined[ids[i]].fetch() | ||
} | ||
this.kicking = false | ||
} | ||
Swarm.prototype._get = function (link) { | ||
@@ -33,9 +47,13 @@ var id = link.toString('hex') | ||
subswarm.feed.on('want', function () { | ||
self.prioritized += subswarm.feed.want.length | ||
subswarm.feed.on('want', function (block) { | ||
self.prioritized++ | ||
debug('prioritizing block %d (%d)', block, self.prioritized) | ||
subswarm.fetch() | ||
}) | ||
subswarm.feed.on('unwant', function () { | ||
subswarm.feed.on('unwant', function (block) { | ||
self.prioritized-- | ||
debug('deprioritizing block %d (%d)', block, self.prioritized) | ||
if (!self.prioritized) subswarm.fetch() | ||
@@ -55,3 +73,3 @@ }) | ||
if (!subswarm.feed.opened) return | ||
debug('[%s] should fetch', self.name) | ||
debug('should try to fetch') | ||
@@ -63,12 +81,14 @@ if (peer) fetchPeer(peer) | ||
function fetchPeer (peer) { | ||
debug('analyzing peer (inflight: %d, choked: %s)', peer.stream.inflight, peer.remoteChoking) | ||
if (peer.remoteChoking) return | ||
while (true) { | ||
if (peer.stream.inflight >= 5) return // max 5 inflight requests | ||
if (peer.stream.inflight >= MAX_INFLIGHT_PER_PEER) return | ||
var block = chooseBlock(peer) | ||
if (block === -1) return | ||
if (block < 0) return self._kick() // nothing to do here - restart logic. TODO: improve me | ||
peer.request(block) | ||
debug('[%s] peer is fetching block %d', self.name, block) | ||
debug('peer is fetching block %d', block) | ||
} | ||
} | ||
function chooseBlock (peer) { | ||
@@ -85,3 +105,3 @@ // TODO: maintain a bitfield of perswarm blocks in progress | ||
if (peer.remoteBitfield.get(block) && !subswarm.feed.bitfield.get(block)) { | ||
debug('[%s] choosing prioritized block #%d', self.name, block) | ||
debug('choosing prioritized block #%d', block) | ||
return block | ||
@@ -94,5 +114,9 @@ } | ||
// when dealing with multiple files | ||
if (self.prioritized && !subswarm.feed.want.length) return -1 | ||
if (self.prioritized && !subswarm.feed.want.length) { | ||
debug('not downloading to yield to prioritized downloading') | ||
return -1 | ||
} | ||
var offset = subswarm.feed.want.length ? subswarm.feed.want[0].block : ((Math.random() * len) | 0) | ||
var prioritizedish = !!subswarm.feed.want.length | ||
var offset = prioritizedish ? subswarm.feed.want[0].block : ((Math.random() * len) | 0) | ||
for (var i = 0; i < len; i++) { | ||
@@ -102,3 +126,4 @@ block = (offset + i) % len | ||
if (peer.remoteBitfield.get(block) && !subswarm.feed.bitfield.get(block)) { | ||
debug('[%s] choosing unprioritized block #%d', self.name, block) | ||
if (!prioritizedish) debug('choosing unprioritized block #%d', block) | ||
else debug('choosing semi prioritized block #%d', block) | ||
return block | ||
@@ -108,3 +133,4 @@ } | ||
return -1 | ||
debug('could not find a block to download') | ||
return -2 | ||
} | ||
@@ -128,3 +154,3 @@ } | ||
debug('[%s] new peer stream', this.name) | ||
debug('new peer stream') | ||
@@ -154,3 +180,3 @@ peer.on('channel', onchannel) | ||
function onchannel (ch) { | ||
var name = ch.link.toString('hex').slice(0, 12) + '/' + self.name | ||
var name = ch.link.toString('hex').slice(0, 12) | ||
var subswarm = add(ch) | ||
@@ -157,0 +183,0 @@ |
{ | ||
"name": "hyperdrive", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"description": "A file sharing network based on rabin file chunking and append only feeds of data verified by merkle trees.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46237
1314