bittorrent-dht
Advanced tools
Comparing version 7.8.2 to 7.9.0
@@ -28,3 +28,6 @@ module.exports = DHT | ||
this._values = LRU(opts.maxValues || 1000) | ||
this._peers = new PeerStore(opts.maxPeers || 10000) | ||
this._peers = new PeerStore({ | ||
maxAge: opts.maxAge || Infinity, | ||
max: opts.maxPeers || 10000 | ||
}) | ||
@@ -782,4 +785,6 @@ this._secrets = null | ||
function PeerStore (max) { | ||
this.max = max || 10000 | ||
function PeerStore (opts) { | ||
if (!opts) opts = {} | ||
this.max = opts.max || 10000 | ||
this.maxAge = opts.maxAge || Infinity | ||
this.used = 0 | ||
@@ -801,5 +806,9 @@ this.peers = LRU(Infinity) | ||
var id = peer.toString('hex') | ||
if (peers.map.get(id)) return | ||
var node = peers.map.get(id) | ||
if (node) { | ||
node.modified = Date.now() | ||
return | ||
} | ||
var node = {index: peers.values.length, peer: peer} | ||
node = {index: peers.values.length, peer: peer, modified: Date.now()} | ||
peers.map.set(id, node) | ||
@@ -823,3 +832,6 @@ peers.values.push(node) | ||
if (!node) return [] | ||
return pick(node.values, 100) | ||
var picked = pick(this, node.values, 100) | ||
if (picked.length) return picked | ||
this.peers.remove(key) | ||
return [] | ||
} | ||
@@ -836,11 +848,19 @@ | ||
function pick (values, n) { | ||
var len = Math.min(values.length, n) | ||
function pick (self, values, n) { | ||
var ptr = 0 | ||
var res = new Array(len) | ||
var res = [] | ||
var now = Date.now() | ||
for (var i = 0; i < len; i++) { | ||
while (values.length && res.length < n && ptr < values.length) { | ||
var next = ptr + (Math.random() * (values.length - ptr)) | 0 | ||
res[ptr] = values[next].peer | ||
swap(values, ptr++, next) | ||
var val = values[next] | ||
if (now - val.modified < self.maxAge) { | ||
res.push(val.peer) | ||
swap(values, ptr++, next) | ||
} else { | ||
swap(values, values.length - 1, next) | ||
values.pop() | ||
self.used-- | ||
} | ||
} | ||
@@ -847,0 +867,0 @@ |
{ | ||
"name": "bittorrent-dht", | ||
"description": "Simple, robust, BitTorrent DHT implementation", | ||
"version": "7.8.2", | ||
"version": "7.9.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "WebTorrent, LLC", |
@@ -84,3 +84,5 @@ # bittorrent-dht [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] [![Greenkeeper badge][greenkeeper-image]][greenkeeper-url] | ||
hash: Function, // custom hash function to use (Function, SHA1 by default), | ||
krpc: krpc() // optional k-rpc instance | ||
krpc: krpc(), // optional k-rpc instance | ||
timeBucketOutdated: 900000, // check buckets every 15min | ||
maxAge: Infinity // optional setting for announced peers to time out | ||
} | ||
@@ -87,0 +89,0 @@ ``` |
@@ -63,1 +63,99 @@ var common = require('./common') | ||
}) | ||
test('`announce` and no cache timeout', function (t) { | ||
t.plan(2) | ||
var dht1 = new DHT({ bootstrap: false, maxAge: Infinity }) | ||
var infoHash = common.randomId() | ||
dht1.listen(function () { | ||
var dht2 = new DHT({ bootstrap: '127.0.0.1:' + dht1.address().port, maxAge: Infinity }) | ||
var cnt = 0 | ||
dht1.on('peer', function (peer) { | ||
cnt++ | ||
}) | ||
dht1.once('announce', function (peer) { | ||
t.deepEqual(peer, {host: '127.0.0.1', port: 1337}) | ||
dht1.lookup(infoHash, function () { | ||
setTimeout(function () { | ||
dht1.lookup(infoHash, function () { | ||
t.equal(cnt, 2, 'finds peers two times') | ||
dht1.destroy() | ||
dht2.destroy() | ||
}) | ||
}, 100) | ||
}) | ||
}) | ||
dht2.announce(infoHash, 1337) | ||
}) | ||
}) | ||
test('`announce` and cache timeout', function (t) { | ||
t.plan(2) | ||
var dht1 = new DHT({ bootstrap: false, maxAge: 50 }) | ||
var infoHash = common.randomId() | ||
dht1.listen(function () { | ||
var dht2 = new DHT({ bootstrap: '127.0.0.1:' + dht1.address().port, maxAge: 50 }) | ||
var cnt = 0 | ||
dht1.on('peer', function (peer) { | ||
cnt++ | ||
}) | ||
dht1.once('announce', function (peer) { | ||
t.deepEqual(peer, {host: '127.0.0.1', port: 1337}) | ||
dht1.lookup(infoHash, function () { | ||
setTimeout(function () { | ||
dht1.lookup(infoHash, function () { | ||
t.equal(cnt, 1, 'just found a peer one time') | ||
dht1.destroy() | ||
dht2.destroy() | ||
}) | ||
}, 100) | ||
}) | ||
}) | ||
dht2.announce(infoHash, 1337) | ||
}) | ||
}) | ||
test('`announce` twice and cache timeout for one announce', function (t) { | ||
var dht1 = new DHT({ bootstrap: false, maxAge: 50 }) | ||
var infoHash = common.randomId() | ||
dht1.listen(function () { | ||
var dht2 = new DHT({ bootstrap: '127.0.0.1:' + dht1.address().port, maxAge: 50 }) | ||
dht2.announce(infoHash, 1337, function () { | ||
dht2.announce(infoHash, 1338, function () { | ||
var found = {} | ||
var interval = setInterval(function () { | ||
dht2.announce(infoHash, 1338) | ||
}, 10) | ||
dht2.on('peer', function (peer) { | ||
found[peer.host + ':' + peer.port] = true | ||
}) | ||
dht2.lookup(infoHash, function () { | ||
t.same(found, {'127.0.0.1:1337': true, '127.0.0.1:1338': true}, 'found two peers') | ||
found = {} | ||
setTimeout(function () { | ||
dht2.lookup(infoHash, function () { | ||
t.same(found, {'127.0.0.1:1338': true}, 'found one peer') | ||
clearInterval(interval) | ||
dht1.destroy() | ||
dht2.destroy() | ||
t.end() | ||
}) | ||
}, 100) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
95562
2532
380