Skip to content

Commit

Permalink
peers: handle maxAge option
Browse files Browse the repository at this point in the history
handle option maxAge to remove stale peers from peercache
  • Loading branch information
robertkowalski committed Jan 10, 2018
1 parent 29aa07c commit 64c9610
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

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

0 comments on commit 64c9610

Please sign in to comment.