diff --git a/README.md b/README.md index 6e6fc9de..c28cf2fc 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,8 @@ If `opts` is specified, then the default options (shown below) will be overridde concurrency: 16, // k-rpc option to specify maximum concurrent UDP requests allowed (Number, 16 by default) hash: Function, // custom hash function to use (Function, SHA1 by default), krpc: krpc(), // optional k-rpc instance - timeBucketOutdated: 900000 // check buckets every 15min + timeBucketOutdated: 900000, // check buckets every 15min + maxAge: Infinity // optional setting for announced peers to time out } ``` diff --git a/client.js b/client.js index 0d496e02..57024313 100644 --- a/client.js +++ b/client.js @@ -25,7 +25,10 @@ function DHT (opts) { this._tables = LRU({maxAge: ROTATE_INTERVAL, max: opts.maxTables || 1000}) 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 + }) this._secrets = null this._hash = opts.hash || sha1 @@ -778,10 +781,11 @@ function toNode (node) { } } -function PeerStore (max) { - this.max = max || 10000 +function PeerStore (opts) { + this.max = opts.max || 10000 + this.maxAge = opts.maxAge || Infinity this.used = 0 - this.peers = LRU(Infinity) + this.peers = LRU({max: Infinity, maxAge: this.maxAge}) } PeerStore.prototype.add = function (key, peer) { @@ -790,7 +794,7 @@ PeerStore.prototype.add = function (key, peer) { if (!peers) { peers = { values: [], - map: LRU(Infinity) + map: LRU({max: Infinity, maxAge: this.maxAge}) } this.peers.set(key, peers) } diff --git a/test/announce.js b/test/announce.js index 6d4f7d92..7e5a797b 100644 --- a/test/announce.js +++ b/test/announce.js @@ -60,3 +60,67 @@ test('announce with implied port', function (t) { }) }) }) + +test('`announce` with {host: "127.0.0.1"} 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, (er, count) => { + + setTimeout(function () { + dht1.lookup(infoHash, (er, c) => { + t.equal(cnt, 2, 'finds peers two times') + dht1.destroy() + dht2.destroy() + }) + }, 100) + }) + }) + + dht2.announce(infoHash, 1337) + }) +}) + +test('`announce` with {host: "127.0.0.1"} 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, (er, count) => { + + setTimeout(function () { + dht1.lookup(infoHash, (er, c) => { + t.equal(cnt, 1, 'just found a peer one time') + dht1.destroy() + dht2.destroy() + }) + }, 100) + }) + }) + + dht2.announce(infoHash, 1337) + }) +})