bittorrent-dht
Advanced tools
Comparing version 7.8.0 to 7.8.1
@@ -43,4 +43,4 @@ module.exports = DHT | ||
this._interval = setInterval(rotateSecrets, ROTATE_INTERVAL) | ||
this._hash = opts.hash || sha1 | ||
this._bucketCheckInterval = null | ||
this._runningBucketCheck = false | ||
this._bucketCheckTimeout = null | ||
this._bucketOutdatedTimeSpan = opts.timeBucketOutdated || BUCKET_OUTDATED_TIMESPAN | ||
@@ -105,19 +105,37 @@ | ||
this._bucketCheckInterval = setInterval(function () { | ||
this._runningBucketCheck = true | ||
queueNext() | ||
function checkBucket () { | ||
const diff = Date.now() - self._rpc.nodes.metadata.lastChange | ||
if (diff >= self._bucketOutdatedTimeSpan) { | ||
self._checkAndRemoveNodes(self.nodes.toArray(), function () { | ||
if (self.nodes.toArray().length < 1) { | ||
// node is currently isolated, | ||
// retry with initial bootstrap nodes | ||
self._bootstrap(true) | ||
} | ||
}) | ||
} | ||
}, interval) | ||
if (diff < self._bucketOutdatedTimeSpan) return queueNext() | ||
self._pingAll(function () { | ||
if (self.destroyed) return | ||
if (self.nodes.toArray().length < 1) { | ||
// node is currently isolated, | ||
// retry with initial bootstrap nodes | ||
self._bootstrap(true) | ||
} | ||
queueNext() | ||
}) | ||
} | ||
function queueNext () { | ||
if (!self._runningBucketCheck || self.destroyed) return | ||
var nextTimeout = Math.floor(Math.random() * interval + interval / 2) | ||
self._bucketCheckTimeout = setTimeout(checkBucket, nextTimeout) | ||
} | ||
} | ||
DHT.prototype._pingAll = function (cb) { | ||
this._checkAndRemoveNodes(this.nodes.toArray(), cb) | ||
} | ||
DHT.prototype.removeBucketCheckInterval = function () { | ||
clearInterval(this._bucketCheckInterval) | ||
this._runningBucketCheck = false | ||
clearTimeout(this._bucketCheckTimeout) | ||
} | ||
@@ -192,2 +210,3 @@ | ||
var self = this | ||
var expectedId = node.id | ||
this._rpc.query(node, {q: 'ping'}, function (err, pong, node) { | ||
@@ -198,2 +217,6 @@ if (err) return cb(err) | ||
} | ||
if (Buffer.isBuffer(expectedId) && !expectedId.equals(pong.r.id)) { | ||
return cb(new Error('Unexpected node id')) | ||
} | ||
self.updateBucketTimestamp() | ||
@@ -465,3 +488,3 @@ cb(null, { | ||
clearInterval(this._interval) | ||
clearInterval(this._bucketCheckInterval) | ||
this.removeBucketCheckInterval() | ||
this._debug('destroying') | ||
@@ -468,0 +491,0 @@ this._rpc.destroy(function () { |
{ | ||
"name": "bittorrent-dht", | ||
"description": "Simple, robust, BitTorrent DHT implementation", | ||
"version": "7.8.0", | ||
"version": "7.8.1", | ||
"author": { | ||
@@ -19,3 +19,3 @@ "name": "WebTorrent, LLC", | ||
"k-bucket": "^3.3.0", | ||
"k-rpc": "^4.1.0", | ||
"k-rpc": "^4.2.1", | ||
"lru": "^3.1.0", | ||
@@ -22,0 +22,0 @@ "randombytes": "^2.0.5", |
@@ -21,3 +21,3 @@ var common = require('./common') | ||
dht.destroy() | ||
}, 1) | ||
}, 50) | ||
}) | ||
@@ -24,0 +24,0 @@ |
91881
26
2419
Updatedk-rpc@^4.2.1