From 64c9610f3b390f7df634c35248ff868674584641 Mon Sep 17 00:00:00 2001 From: Robert Kowalski Date: Wed, 10 Jan 2018 14:28:25 +0100 Subject: [PATCH] peers: handle maxAge option handle option maxAge to remove stale peers from peercache --- README.md | 3 ++- client.js | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) 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) }