Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
chore: add max num providers to find providers
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 12, 2018
1 parent 3a5581b commit cccde59
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ class KadDHT {
* @param {CID} key
* @param {Object} options - findProviders options
* @param {number} options.maxTimeout - how long the query should maximally run, in milliseconds (default: 60000)
* @param {number} options.maxNumProviders - maximum number of providers to find
* @param {function(Error, Array<PeerInfo>)} callback
* @returns {void}
*/
Expand All @@ -471,12 +472,11 @@ class KadDHT {
options = options || {}
}

if (!options.maxTimeout) {
options.maxTimeout = c.minute
}
options.maxTimeout = options.maxTimeout || c.minute
options.maxNumProviders = options.maxNumProviders || c.K

this._log('findProviders %s', key.toBaseEncodedString())
this._findNProviders(key, options.maxTimeout, c.K, callback)
this._findNProviders(key, options.maxTimeout, options.maxNumProviders, callback)
}

// ----------- Peer Routing
Expand Down
31 changes: 31 additions & 0 deletions test/kad-dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,37 @@ describe('KadDHT', () => {
})
})

it('find providers', function (done) {
this.timeout(20 * 1000)

const val = values[0]
const tdht = new TestDHT()

tdht.spawn(3, (err, dhts) => {
expect(err).to.not.exist()

series([
(cb) => connect(dhts[0], dhts[1], cb),
(cb) => connect(dhts[1], dhts[2], cb),
(cb) => each(dhts, (dht, cb) => dht.provide(val.cid, cb), cb),
(cb) => dhts[0].findProviders(val.cid, {}, cb),
(cb) => dhts[0].findProviders(val.cid, { maxNumProviders: 2 }, cb)
], (err, res) => {
expect(err).to.not.exist()

// find providers find all the 3 providers
expect(res[3]).to.exist()
expect(res[3]).to.have.length(3)

// find providers limited to a maxium of 2 providers
expect(res[4]).to.exist()
expect(res[4]).to.have.length(2)

done()
})
})
})

it('random-walk', function (done) {
this.timeout(40 * 1000)

Expand Down

0 comments on commit cccde59

Please sign in to comment.