Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

[WIP] feat: handle stream muxer errors #245

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 11 additions & 6 deletions src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ function dial (swtch) {
// 3. update the peerInfo that is already stored in the conn
}

openConnInMuxedConn(muxer, (conn) => {
openConnInMuxedConn(muxer, (err, conn) => {
if (err) {
return callback(err)
}
protocolHandshake(conn, protocol, callback)
})
}
Expand All @@ -89,18 +92,20 @@ function dial (swtch) {

const tKeys = swtch.availableTransports(pi)

const circuitEnabled = Boolean(swtch.transports[Circuit.tag])
let circuitTried = false
nextTransport(tKeys.shift())

function nextTransport (key) {
let transport = key
if (!transport) {
if (circuitTried) {
return cb(new Error(`Circuit already tried!`))
if (!circuitEnabled) {
const msg = `Circuit not enabled and all transports failed to dial peer ${pi.id.toB58String()}!`
return cb(new Error(msg))
}

if (!swtch.transports[Circuit.tag]) {
return cb(new Error(`Circuit not enabled!`))
if (circuitTried) {
return cb(new Error(`No available transports to dial peer ${pi.id.toB58String()}!`))
}

log(`Falling back to dialing over circuit`)
Expand Down Expand Up @@ -208,7 +213,7 @@ function dial (swtch) {
}

function openConnInMuxedConn (muxer, cb) {
cb(muxer.newStream())
return muxer.newStream(cb)
}

function protocolHandshake (conn, protocol, cb) {
Expand Down
2 changes: 1 addition & 1 deletion test/circuit-relay.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe(`circuit`, function () {

swarmA.dial(swarmC._peerInfo, (err, conn) => {
expect(err).to.exist()
expect(err).to.match(/Circuit already tried!/)
expect(err).to.match(/No available transports to dial peer/)
expect(conn).to.not.exist()
expect(dialSpyA.callCount).to.be.eql(1)
done()
Expand Down
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ require('./swarm-no-muxing.node')
require('./swarm-muxing.node')
require('./circuit-relay.node')
require('./limit-dialer.node')
require('./stats.node')
2 changes: 1 addition & 1 deletion test/stats.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const each = require('async/each')
const map = require('async/map')
const series = require('async/series')
const TCP = require('libp2p-tcp')
const multiplex = require('libp2p-multiplex')
const multiplex = require('libp2p-mplex')
const pull = require('pull-stream')
const secio = require('libp2p-secio')
const PeerBook = require('peer-book')
Expand Down
16 changes: 16 additions & 0 deletions test/stream-muxers.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Stream Multiplexing', () => {

before((done) => createInfos(3, (err, peerInfos) => {
expect(err).to.not.exist()

function maGen (port) { return `/ip4/127.0.0.1/tcp/${port}` }

const peerA = peerInfos[0]
Expand Down Expand Up @@ -149,5 +150,20 @@ describe('Stream Multiplexing', () => {
}, 500)
})
})

it('error propagates correctly from multiplexer', function (done) {
const id = switchA._peerInfo.id.toB58String()
const muxer = switchB.muxedConns[id].muxer
muxer.newStream = (cb) => {
cb(new Error())
}

switchB.dial(switchA._peerInfo, '/papaia/1.0.0', (err, conn) => {
expect(err).to.exist()
expect(err).to.be.an.instanceof(Error)
expect(conn).to.not.exist()
done()
})
})
}))
})