Skip to content

Commit

Permalink
peers: handle maxAge option (#186)
Browse files Browse the repository at this point in the history
* docs: document bucket timeout option

* peers: handle maxAge option

handle option maxAge to remove stale peers from peercache
  • Loading branch information
robertkowalski authored and mafintosh committed Jan 10, 2018
1 parent c2cb220 commit 310a012
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ If `opts` is specified, then the default options (shown below) will be overridde
host: false, // host of local peer, if specified then announces get added to local table (String, disabled by default)
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
krpc: krpc(), // optional k-rpc instance
timeBucketOutdated: 900000, // check buckets every 15min
maxAge: Infinity // optional setting for announced peers to time out
}
```

Expand Down
14 changes: 9 additions & 5 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down
64 changes: 64 additions & 0 deletions test/announce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit 310a012

Please sign in to comment.