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

Commit 3046b54

Browse files
authored
chore: update options timeout property (#62)
* chore: update options timeout property BREAKING CHANGE: get, getMany, findProviders and findPeer do not accept a timeout number anymore. It must be a property of an object options. Co-Authored-By: vasco-santos <[email protected]>
1 parent d645235 commit 3046b54

File tree

4 files changed

+50
-55
lines changed

4 files changed

+50
-55
lines changed

src/index.js

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -203,24 +203,22 @@ class KadDHT {
203203
*
204204
* @param {Buffer} key
205205
* @param {Object} options - get options
206-
* @param {number} options.maxTimeout - optional timeout (default: 60000)
206+
* @param {number} options.timeout - optional timeout (default: 60000)
207207
* @param {function(Error, Buffer)} callback
208208
* @returns {void}
209209
*/
210210
get (key, options, callback) {
211211
if (typeof options === 'function') {
212212
callback = options
213213
options = {}
214-
} else if (typeof options === 'number') { // This will be deprecated in a next release
215-
options = {
216-
maxTimeout: options
217-
}
218214
} else {
219215
options = options || {}
220216
}
221217

222-
if (!options.maxTimeout) {
223-
options.maxTimeout = c.minute
218+
if (!options.maxTimeout && !options.timeout) {
219+
options.timeout = c.minute // default
220+
} else if (options.maxTimeout && !options.timeout) { // TODO this will be deprecated in a next release
221+
options.timeout = options.maxTimeout
224222
}
225223

226224
this._get(key, options, callback)
@@ -232,24 +230,22 @@ class KadDHT {
232230
* @param {Buffer} key
233231
* @param {number} nvals
234232
* @param {Object} options - get options
235-
* @param {number} options.maxTimeout - optional timeout (default: 60000)
233+
* @param {number} options.timeout - optional timeout (default: 60000)
236234
* @param {function(Error, Array<{from: PeerId, val: Buffer}>)} callback
237235
* @returns {void}
238236
*/
239237
getMany (key, nvals, options, callback) {
240238
if (typeof options === 'function') {
241239
callback = options
242240
options = {}
243-
} else if (typeof options === 'number') { // This will be deprecated in a next release
244-
options = {
245-
maxTimeout: options
246-
}
247241
} else {
248242
options = options || {}
249243
}
250244

251-
if (!options.maxTimeout) {
252-
options.maxTimeout = c.minute
245+
if (!options.maxTimeout && !options.timeout) {
246+
options.timeout = 'c.minute' // default
247+
} else if (options.maxTimeout && !options.timeout) { // TODO this will be deprecated in a next release
248+
options.timeout = options.maxTimeout
253249
}
254250

255251
this._log('getMany %b (%s)', key, nvals)
@@ -322,7 +318,7 @@ class KadDHT {
322318
})
323319

324320
// run our query
325-
timeout((cb) => query.run(rtp, cb), options.maxTimeout)(cb)
321+
timeout((cb) => query.run(rtp, cb), options.timeout)(cb)
326322
}
327323
], (err) => {
328324
// combine vals from each path
@@ -485,7 +481,7 @@ class KadDHT {
485481
*
486482
* @param {CID} key
487483
* @param {Object} options - findProviders options
488-
* @param {number} options.maxTimeout - how long the query should maximally run, in milliseconds (default: 60000)
484+
* @param {number} options.timeout - how long the query should maximally run, in milliseconds (default: 60000)
489485
* @param {number} options.maxNumProviders - maximum number of providers to find
490486
* @param {function(Error, Array<PeerInfo>)} callback
491487
* @returns {void}
@@ -494,19 +490,20 @@ class KadDHT {
494490
if (typeof options === 'function') {
495491
callback = options
496492
options = {}
497-
} else if (typeof options === 'number') { // This will be deprecated in a next release
498-
options = {
499-
maxTimeout: options
500-
}
501493
} else {
502494
options = options || {}
503495
}
504496

505-
options.maxTimeout = options.maxTimeout || c.minute
497+
if (!options.maxTimeout && !options.timeout) {
498+
options.timeout = c.minute // default
499+
} else if (options.maxTimeout && !options.timeout) { // TODO this will be deprecated in a next release
500+
options.timeout = options.maxTimeout
501+
}
502+
506503
options.maxNumProviders = options.maxNumProviders || c.K
507504

508505
this._log('findProviders %s', key.toBaseEncodedString())
509-
this._findNProviders(key, options.maxTimeout, options.maxNumProviders, callback)
506+
this._findNProviders(key, options.timeout, options.maxNumProviders, callback)
510507
}
511508

512509
// ----------- Peer Routing
@@ -516,24 +513,22 @@ class KadDHT {
516513
*
517514
* @param {PeerId} id
518515
* @param {Object} options - findPeer options
519-
* @param {number} options.maxTimeout - how long the query should maximally run, in milliseconds (default: 60000)
516+
* @param {number} options.timeout - how long the query should maximally run, in milliseconds (default: 60000)
520517
* @param {function(Error, PeerInfo)} callback
521518
* @returns {void}
522519
*/
523520
findPeer (id, options, callback) {
524521
if (typeof options === 'function') {
525522
callback = options
526523
options = {}
527-
} else if (typeof options === 'number') { // This will be deprecated in a next release
528-
options = {
529-
maxTimeout: options
530-
}
531524
} else {
532525
options = options || {}
533526
}
534527

535-
if (!options.maxTimeout) {
536-
options.maxTimeout = c.minute
528+
if (!options.maxTimeout && !options.timeout) {
529+
options.timeout = c.minute // default
530+
} else if (options.maxTimeout && !options.timeout) { // TODO this will be deprecated in a next release
531+
options.timeout = options.maxTimeout
537532
}
538533

539534
this._log('findPeer %s', id.toB58String())
@@ -594,7 +589,7 @@ class KadDHT {
594589

595590
timeout((cb) => {
596591
query.run(peers, cb)
597-
}, options.maxTimeout)(cb)
592+
}, options.timeout)(cb)
598593
},
599594
(result, cb) => {
600595
let success = false

src/private.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ module.exports = (dht) => ({
260260
*
261261
* @param {Buffer} key
262262
* @param {Object} options - get options
263-
* @param {number} options.maxTimeout - optional timeout (default: 60000)
263+
* @param {number} options.timeout - optional timeout (default: 60000)
264264
* @param {function(Error, Record)} callback
265265
* @returns {void}
266266
*
@@ -269,7 +269,7 @@ module.exports = (dht) => ({
269269
_get (key, options, callback) {
270270
dht._log('_get %b', key)
271271
waterfall([
272-
(cb) => dht.getMany(key, 16, options.maxTimeout, cb),
272+
(cb) => dht.getMany(key, 16, options, cb),
273273
(vals, cb) => {
274274
const recs = vals.map((v) => v.val)
275275
let i = 0
@@ -458,14 +458,14 @@ module.exports = (dht) => ({
458458
* Search the dht for up to `n` providers of the given CID.
459459
*
460460
* @param {CID} key
461-
* @param {number} maxTimeout - How long the query should maximally run in milliseconds.
461+
* @param {number} providerTimeout - How long the query should maximally run in milliseconds.
462462
* @param {number} n
463463
* @param {function(Error, Array<PeerInfo>)} callback
464464
* @returns {void}
465465
*
466466
* @private
467467
*/
468-
_findNProviders (key, maxTimeout, n, callback) {
468+
_findNProviders (key, providerTimeout, n, callback) {
469469
let out = new LimitedPeerList(n)
470470

471471
dht.providers.getProviders(key, (err, provs) => {
@@ -524,7 +524,7 @@ module.exports = (dht) => ({
524524

525525
const peers = dht.routingTable.closestPeers(key.buffer, c.ALPHA)
526526

527-
timeout((cb) => query.run(peers, cb), maxTimeout)((err) => {
527+
timeout((cb) => query.run(peers, cb), providerTimeout)((err) => {
528528
// combine peers from each path
529529
paths.forEach((path) => {
530530
path.toArray().forEach((peer) => {

src/random-walk.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ class RandomWalk {
2525
*
2626
* @param {number} [queries=1] - how many queries to run per period
2727
* @param {number} [period=300000] - how often to run the the random-walk process, in milliseconds (5min)
28-
* @param {number} [maxTimeout=10000] - how long to wait for the the random-walk query to run, in milliseconds (10s)
28+
* @param {number} [timeout=10000] - how long to wait for the the random-walk query to run, in milliseconds (10s)
2929
* @returns {void}
3030
*/
31-
start (queries, period, maxTimeout) {
31+
start (queries, period, timeout) {
3232
if (queries == null) { queries = 1 }
3333
if (period == null) { period = 5 * c.minute }
34-
if (maxTimeout == null) { maxTimeout = 10 * c.second }
34+
if (timeout == null) { timeout = 10 * c.second }
3535
// Don't run twice
3636
if (this._running) { return }
3737

@@ -66,7 +66,7 @@ class RandomWalk {
6666

6767
// Start runner
6868
runningHandle.runPeriodically((done) => {
69-
this._walk(queries, maxTimeout, () => done(period))
69+
this._walk(queries, timeout, () => done(period))
7070
}, period)
7171
this._runningHandle = runningHandle
7272
}
@@ -92,21 +92,21 @@ class RandomWalk {
9292
* Do the random walk work.
9393
*
9494
* @param {number} queries
95-
* @param {number} maxTimeout
95+
* @param {number} walkTimeout
9696
* @param {function(Error)} callback
9797
* @returns {void}
9898
*
9999
* @private
100100
*/
101-
_walk (queries, maxTimeout, callback) {
101+
_walk (queries, walkTimeout, callback) {
102102
this._kadDHT._log('random-walk:start')
103103

104104
times(queries, (i, cb) => {
105105
waterfall([
106106
(cb) => this._randomPeerId(cb),
107107
(id, cb) => timeout((cb) => {
108108
this._query(id, cb)
109-
}, maxTimeout)(cb)
109+
}, walkTimeout)(cb)
110110
], (err) => {
111111
if (err) {
112112
this._kadDHT._log.error('random-walk:error', err)

test/kad-dht.spec.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function bootstrap (dhts) {
7777
})
7878
}
7979

80-
function waitForWellFormedTables (dhts, minPeers, avgPeers, maxTimeout, callback) {
80+
function waitForWellFormedTables (dhts, minPeers, avgPeers, waitTimeout, callback) {
8181
timeout((cb) => {
8282
retry({ times: 50, interval: 200 }, (cb) => {
8383
let totalPeers = 0
@@ -98,7 +98,7 @@ function waitForWellFormedTables (dhts, minPeers, avgPeers, maxTimeout, callback
9898
const done = ready.every(Boolean)
9999
cb(done ? null : new Error('not done yet'))
100100
}, cb)
101-
}, maxTimeout)(callback)
101+
}, waitTimeout)(callback)
102102
}
103103

104104
function countDiffPeers (a, b) {
@@ -267,7 +267,7 @@ describe('KadDHT', () => {
267267
waterfall([
268268
(cb) => connect(dhtA, dhtB, cb),
269269
(cb) => dhtA.put(Buffer.from('/v/hello'), Buffer.from('world'), cb),
270-
(cb) => dhtB.get(Buffer.from('/v/hello'), { maxTimeout: 1000 }, cb),
270+
(cb) => dhtB.get(Buffer.from('/v/hello'), { timeout: 1000 }, cb),
271271
(res, cb) => {
272272
expect(res).to.eql(Buffer.from('world'))
273273
cb()
@@ -291,7 +291,7 @@ describe('KadDHT', () => {
291291
waterfall([
292292
(cb) => connect(dhtA, dhtB, cb),
293293
(cb) => dhtA.put(Buffer.from('hello'), Buffer.from('world'), cb),
294-
(cb) => dhtB.get(Buffer.from('hello'), { maxTimeout: 1000 }, cb),
294+
(cb) => dhtB.get(Buffer.from('hello'), { timeout: 1000 }, cb),
295295
(res, cb) => {
296296
expect(res).to.eql(Buffer.from('world'))
297297
cb()
@@ -324,7 +324,7 @@ describe('KadDHT', () => {
324324
waterfall([
325325
(cb) => connect(dhtA, dhtB, cb),
326326
(cb) => dhtA.put(Buffer.from('/ipns/hello'), Buffer.from('world'), cb),
327-
(cb) => dhtB.get(Buffer.from('/ipns/hello'), { maxTimeout: 1000 }, cb),
327+
(cb) => dhtB.get(Buffer.from('/ipns/hello'), { timeout: 1000 }, cb),
328328
(res, cb) => {
329329
expect(res).to.eql(Buffer.from('world'))
330330
cb()
@@ -348,7 +348,7 @@ describe('KadDHT', () => {
348348
waterfall([
349349
(cb) => connect(dhtA, dhtB, cb),
350350
(cb) => dhtA.put(Buffer.from('/v2/hello'), Buffer.from('world'), cb),
351-
(cb) => dhtB.get(Buffer.from('/v2/hello'), { maxTimeout: 1000 }, cb)
351+
(cb) => dhtB.get(Buffer.from('/v2/hello'), { timeout: 1000 }, cb)
352352
], (err) => {
353353
expect(err).to.exist()
354354
expect(err.code).to.eql('ERR_UNRECOGNIZED_KEY_PREFIX')
@@ -376,8 +376,8 @@ describe('KadDHT', () => {
376376
expect(err).to.not.exist()
377377

378378
series([
379-
(cb) => dhtA.get(Buffer.from('/v/hello'), { maxTimeout: 1000 }, cb),
380-
(cb) => dhtB.get(Buffer.from('/v/hello'), { maxTimeout: 1000 }, cb)
379+
(cb) => dhtA.get(Buffer.from('/v/hello'), { timeout: 1000 }, cb),
380+
(cb) => dhtB.get(Buffer.from('/v/hello'), { timeout: 1000 }, cb)
381381
], (err, results) => {
382382
expect(err).to.not.exist()
383383
results.forEach((res) => {
@@ -412,7 +412,7 @@ describe('KadDHT', () => {
412412
let n = 0
413413
each(values, (v, cb) => {
414414
n = (n + 1) % 3
415-
dhts[n].findProviders(v.cid, { maxTimeout: 5000 }, (err, provs) => {
415+
dhts[n].findProviders(v.cid, { timeout: 5000 }, (err, provs) => {
416416
expect(err).to.not.exist()
417417
expect(provs).to.have.length(1)
418418
expect(provs[0].id.id).to.be.eql(ids[3].id)
@@ -507,7 +507,7 @@ describe('KadDHT', () => {
507507
Buffer.from('world'),
508508
cb
509509
),
510-
(cb) => dhts[0].get(Buffer.from('/v/hello'), { maxTimeout: 1000 }, cb),
510+
(cb) => dhts[0].get(Buffer.from('/v/hello'), { timeout: 1000 }, cb),
511511
(res, cb) => {
512512
expect(res).to.eql(Buffer.from('world'))
513513
cb()
@@ -534,7 +534,7 @@ describe('KadDHT', () => {
534534
(cb) => connect(dhts[0], dhts[1], cb),
535535
(cb) => connect(dhts[1], dhts[2], cb),
536536
(cb) => connect(dhts[2], dhts[3], cb),
537-
(cb) => dhts[0].findPeer(ids[3], { maxTimeout: 1000 }, cb),
537+
(cb) => dhts[0].findPeer(ids[3], { timeout: 1000 }, cb),
538538
(res, cb) => {
539539
expect(res.id.isEqual(ids[3])).to.eql(true)
540540
cb()
@@ -878,7 +878,7 @@ describe('KadDHT', () => {
878878

879879
waterfall([
880880
(cb) => connect(dhtA, dhtB, cb),
881-
(cb) => dhtA.get(Buffer.from('/v/hello'), { maxTimeout: 1000 }, cb)
881+
(cb) => dhtA.get(Buffer.from('/v/hello'), { timeout: 1000 }, cb)
882882
], (err) => {
883883
expect(err).to.exist()
884884
expect(err.code).to.be.eql(errCode)
@@ -936,7 +936,7 @@ describe('KadDHT', () => {
936936
expect(err).to.not.exist()
937937
const stub = sinon.stub(dhts[0].routingTable, 'closestPeers').returns([])
938938

939-
dhts[0].findPeer(ids[3], { maxTimeout: 1000 }, (err) => {
939+
dhts[0].findPeer(ids[3], { timeout: 1000 }, (err) => {
940940
expect(err).to.exist()
941941
expect(err.code).to.eql('ERR_LOOKUP_FAILED')
942942
stub.restore()

0 commit comments

Comments
 (0)