From 0d97bb0321cbb8587e6d4f8007faa72ce989d24b Mon Sep 17 00:00:00 2001 From: mkg20001 Date: Sun, 10 Sep 2017 10:11:59 +0200 Subject: [PATCH] Add discovery drain event - Fix #93 --- zeronet-zite/lib/file/stream.js | 10 ++-------- zeronet-zite/lib/index.js | 2 +- zeronet-zite/lib/pool/stream.js | 14 ++++++++++++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/zeronet-zite/lib/file/stream.js b/zeronet-zite/lib/file/stream.js index ce1df0e..4f990de 100644 --- a/zeronet-zite/lib/file/stream.js +++ b/zeronet-zite/lib/file/stream.js @@ -19,7 +19,6 @@ module.exports = function FileStream(data) { let dlpath = data.path + "@" + data.site let othersize = 0 - //let fullchunk = [] log("init", dlpath, info) @@ -35,13 +34,11 @@ module.exports = function FileStream(data) { let chunks = [] function finishLoop(err) { - if (chunks.length && err) sendErr = err - if (chunks.length) return cb(null, chunks) - return cb(err) + return cb(err, chunks) } function loop() { - if (cur >= info.size) return finishLoop(!log("finished", dlpath, cur)) + if (cur >= info.size) return finishLoop(true, log("finished", dlpath, cur)) let args = { site: data.site, location: cur, @@ -49,7 +46,6 @@ module.exports = function FileStream(data) { } if (info.size) args.file_size = info.size peer.cmd("getFile", args, function (err, res) { - //console.log("peere", peer.multiaddr, err) if (err) { peer.score -= 20 return finishLoop() //goto: next @@ -58,7 +54,6 @@ module.exports = function FileStream(data) { if (!info.size) return finishLoop() othersize++ if (othersize == 2) { - //require("fs").writeFileSync("/tmp/site-failed-download",Buffer.concat(fullchunk)) return finishLoop(new Error("Other size")) } return finishLoop() @@ -66,7 +61,6 @@ module.exports = function FileStream(data) { if (!info.size) info.size = res.size cur += res.body.length log("downloaded", dlpath, cur, info.size) - //fullchunk.push(res.body) chunks.push(res.body) return loop() }) diff --git a/zeronet-zite/lib/index.js b/zeronet-zite/lib/index.js index cab4c7b..622d57c 100644 --- a/zeronet-zite/lib/index.js +++ b/zeronet-zite/lib/index.js @@ -96,7 +96,7 @@ module.exports = function Zite(config, node) { //describes a single zite i.version = i.authority.version node.logger("node")("Downloaded %s", i.path) }), - tree.storage.writeStream(tree.zite.address, 0, i.path) + tree.storage.writeStream(tree.zite.address, 0, i.path.replace(/^\//, "")) ) }) }, err => err ? console.error(err) : null) diff --git a/zeronet-zite/lib/pool/stream.js b/zeronet-zite/lib/pool/stream.js index 6274b05..4ea0802 100644 --- a/zeronet-zite/lib/pool/stream.js +++ b/zeronet-zite/lib/pool/stream.js @@ -24,11 +24,17 @@ module.exports = function PeerStream(zite) { function PeerList() { //gets peers const getter = new Getters.MetaGetter([new Getters.OnlineGetter(zite.pool), new Getters.OfflineGetter(zite.pool)]) const dgetter = new Getters.MetaGetter([new Getters.DiscoveryCandidateGetter(zite.pool.main, zite.address)]) + let last0 = false + let failedDiscovery = 0 return function (end, cb) { if (end) return cb(end) log("peer:list:out read") + if (!last0) failedDiscovery = 0 + last0 = false + function getLoop() { + if (failedDiscovery >= 3) return cb(new Error("PeerList drained")) if (getter.peers) { if (getter.peers <= 5) zite.discovery.discover() //low peers const peer_ = getter.getSync() @@ -46,12 +52,16 @@ module.exports = function PeerStream(zite) { log("peer:list:out discovered a peer") return cb(null, peer) } else { - log("peer:list:out discovery method fail") + last0 = true + failedDiscovery += 0.1 + log("peer:list:out discovery method fail (f = %s)", failedDiscovery) return getLoop() } }) } else { - log("peer:list:out drained. discover") + log("peer:list:out drained. discover (f = %s)", failedDiscovery) + last0 = true + failedDiscovery += 1 zite.discovery.discoverCB(() => { process.nextTick(getLoop) })