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

Commit

Permalink
refactor(async): _findNProviders query function
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed May 18, 2019
1 parent 042e852 commit ed1a737
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"abort-controller": "^3.0.0",
"async": "^2.6.2",
"base32.js": "~0.1.0",
"callbackify": "^1.1.0",
"chai-checkmark": "^1.0.1",
"cids": "~0.7.0",
"debug": "^4.1.1",
Expand Down
42 changes: 18 additions & 24 deletions src/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const PeerId = require('peer-id')
const libp2pRecord = require('libp2p-record')
const waterfall = require('async/waterfall')
const timeout = require('async/timeout')
const PeerInfo = require('peer-info')
const promisify = require('promisify-es6')
const promiseToCallback = require('promise-to-callback')
const callbackify = require('callbackify')

const errcode = require('err-code')

Expand Down Expand Up @@ -543,29 +543,23 @@ module.exports = (dht) => ({
paths.push(pathProviders)

// Here we return the query function to use on this particular disjoint path
return (peer, cb) => {
waterfall([
(cb) => dht._findProvidersSingle(peer, key, cb),
(msg, cb) => {
const provs = msg.providerPeers
dht._log('(%s) found %s provider entries', dht.peerInfo.id.toB58String(), provs.length)

provs.forEach((prov) => {
pathProviders.push(dht.peerBook.put(prov))
})

// hooray we have all that we want
if (pathProviders.length >= pathSize) {
return cb(null, { pathComplete: true })
}

// it looks like we want some more
cb(null, {
closerPeers: msg.closerPeers
})
}
], cb)
}
return callbackify(async (peer) => {
const msg = await dht._findProvidersSingleAsync(peer, key)
const provs = msg.providerPeers
dht._log('(%s) found %s provider entries', dht.peerInfo.id.toB58String(), provs.length)

provs.forEach((prov) => {
pathProviders.push(dht.peerBook.put(prov))
})

// hooray we have all that we want
if (pathProviders.length >= pathSize) {
return { pathComplete: true }
}

// it looks like we want some more
return { closerPeers: msg.closerPeers }
})
})

const peers = dht.routingTable.closestPeers(key.buffer, dht.kBucketSize)
Expand Down

0 comments on commit ed1a737

Please sign in to comment.