Skip to content

Commit 805d1ad

Browse files
Alan Shawjacobheun
Alan Shaw
authored andcommitted
fix: revert to try each (libp2p#320)
1 parent f31663f commit 805d1ad

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

src/limit-dialer/index.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
'use strict'
22

3-
const race = require('async/race')
3+
const tryEach = require('async/tryEach')
44
const debug = require('debug')
5-
const once = require('once')
65

76
const log = debug('libp2p:switch:dialer')
87

98
const DialQueue = require('./queue')
10-
const { CONNECTION_FAILED } = require('../errors')
119

1210
/**
1311
* Track dials per peer and limited them.
@@ -39,29 +37,26 @@ class LimitDialer {
3937
log('dialMany:start')
4038
// we use a token to track if we want to cancel following dials
4139
const token = { cancel: false }
42-
callback = once(callback) // only call callback once
4340

4441
let errors = []
4542
const tasks = addrs.map((m) => {
46-
return (cb) => this.dialSingle(peer, transport, m, token, (err, res) => {
47-
if (res) return cb(null, res)
48-
49-
errors.push(err || CONNECTION_FAILED())
50-
51-
if (errors.length === tasks.length) {
52-
cb(errors)
43+
return (cb) => this.dialSingle(peer, transport, m, token, (err, result) => {
44+
if (err) {
45+
errors.push(err)
46+
return cb(err)
5347
}
48+
return cb(null, result)
5449
})
5550
})
5651

57-
race(tasks, (_, successfulDial) => {
58-
if (successfulDial) {
52+
tryEach(tasks, (_, result) => {
53+
if (result && result.conn) {
5954
log('dialMany:success')
60-
return callback(null, successfulDial)
55+
return callback(null, result)
6156
}
6257

6358
log('dialMany:error')
64-
return callback(errors)
59+
callback(errors)
6560
})
6661
}
6762

test/limit-dialer.node.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ describe('LimitDialer', () => {
5353
it('two success', (done) => {
5454
const dialer = new LimitDialer(2, 10)
5555

56-
expect(2).checks(done)
57-
5856
// mock transport
5957
const t1 = {
6058
dial (addr, cb) {
@@ -72,10 +70,7 @@ describe('LimitDialer', () => {
7270
setTimeout(cb, 8)
7371
return {
7472
source: pull.values([2]),
75-
sink: pull.onEnd((err) => {
76-
// Verify the unused connection gets closed
77-
expect(err).to.not.exist().mark()
78-
})
73+
sink: pull.drain()
7974
}
8075
}
8176
}
@@ -89,7 +84,8 @@ describe('LimitDialer', () => {
8984
conn,
9085
pull.collect((err, res) => {
9186
expect(err).to.not.exist()
92-
expect(res).to.be.eql([1]).mark()
87+
expect(res).to.be.eql([1])
88+
done()
9389
})
9490
)
9591
})

0 commit comments

Comments
 (0)