Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add timeout defaults #6

Merged
merged 2 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const parallel = require('async/parallel')
const reflect = require('async/reflect')
const multiaddr = require('multiaddr')

const DEFAULT_MAX_TIMEOUT = 30e3 // 30 second default
const DEFAULT_IPFS_API = {
protocol: 'https',
port: 443,
Expand Down Expand Up @@ -57,20 +58,23 @@ class DelegatedContentRouting {
* Search the dht for providers of the given CID.
*
* - call `findProviders` on the delegated node.
* - does not support the `timeout` parameter, as this is specific to the delegate node.
*
* @param {CID} key
* @param {number} _timeout This is ignored and is only present to comply with the dht interface
* @param {number} timeout How long the query can take. Defaults to 30 seconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are changing this, maybe we should change this param to an options object with the timeout property, and for now, keep accepting it as a number, but deprecate in the future, in favor of options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The major reason I didn't make the change to options was to comply with how the dht is doing things, https://github.com/libp2p/js-libp2p-kad-dht/blob/0a2f9fe418d364356d27152be6a7279ebb220d2c/src/index.js#L450. I'm good making the change but we should do that in the dht as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! I am just concerned that we may need to change all them in the future. I Can create a PR for kad-dht adding that behavior, what do you think?

* @param {function(Error, Array<PeerInfo>)} callback
* @returns {void}
*/
findProviders (key, _timeout, callback) {
if (typeof _timeout === 'function') {
callback = _timeout
_timeout = null
findProviders (key, timeout, callback) {
if (typeof timeout === 'function') {
callback = timeout
timeout = null
}

this.dht.findprovs(key.toBaseEncodedString(), (err, results) => {
timeout = timeout || DEFAULT_MAX_TIMEOUT

this.dht.findprovs(key.toBaseEncodedString(), {
timeout: `${timeout}ms` // The api requires specification of the time unit (s/ms)
}, (err, results) => {
if (err) {
return callback(err)
}
Expand Down
25 changes: 25 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ describe('DelegatedContentRouting', function () {
}
], done)
})

it('should be able to specify a timeout', function (done) {
async.waterfall([
(cb) => {
const opts = delegatedNode.apiAddr.toOptions()
const routing = new DelegatedContentRouting(selfId, {
protocol: 'http',
port: opts.port,
host: opts.host
})
const cid = new CID('QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv')
routing.findProviders(cid, 5e3, cb)
},
(providers, cb) => {
// We should get our local node and the bootstrap node as providers.
// The delegate node is not included, because it is handling the requests
expect(providers).to.have.length(2)
expect(providers.map((p) => p.id.toB58String())).to.have.members([
bootstrapId.id,
selfId.toB58String()
])
cb()
}
], done)
})
})

describe('provide', () => {
Expand Down