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 3 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
23 changes: 17 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,13 @@ function dial (swtch) {
}

function openConnInMuxedConn (muxer, cb) {
cb(muxer.newStream())
muxer.newStream((err, conn) => {
Copy link
Member

Choose a reason for hiding this comment

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

This asking to change the Stream Muxer interface. https://github.com/libp2p/interface-stream-muxer needs to be updated.

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't really change newStream interface, it already takes a callback as it is. I reverted this to return a connection as before. This PR just tries to propagate the error if it's returned by the multiplexer - before it would just be ignored.

if (err) {
return cb(err)
}

cb(null, conn)
})
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

Choose a reason for hiding this comment

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

why not just 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
20 changes: 19 additions & 1 deletion test/stream-muxers.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ const Switch = require('../src')
describe('Stream Multiplexing', () => {
[
multiplex,
spdy
spdy // TODO: do we still support this?
Copy link
Member

Choose a reason for hiding this comment

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

Of course we still support. The whole design of libp2p is to support multiple building blocks.

Copy link
Member Author

Choose a reason for hiding this comment

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

spdy doesn't return any errors when the stream abruptly terminates. We should make the behaviour consistent then.

What should happen - error on abrupt termination or silent skip?

].forEach((sm) => describe(sm.multicodec, () => {
let switchA
let switchB
let switchC

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,22 @@ describe('Stream Multiplexing', () => {
}, 500)
})
})

it('should fail graciously on dead stream mux', function (done) {
if (sm.multicodec === '/spdy/3.1.0') {
this.skip()
}
Copy link
Member

Choose a reason for hiding this comment

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

No skipping for SPDY

Copy link
Member Author

Choose a reason for hiding this comment

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

Skipping because the behaviour is not consistent across muxers - spdy doesn't return any errors on abrupt termination.


const id = switchA._peerInfo.id.toB58String()
const muxer = switchB.muxedConns[id].muxer
muxer.multiplex.destroyed = true // simulate a destroyed stream

switchB.dial(switchA._peerInfo, '/papaia/1.0.0', (err, conn) => {
expect(err).to.exist()
expect(err).to.match(/Error: Multiplexer is destroyed/)
muxer.multiplex.destroyed = false // re-enable stream to avoid hangs
done()
})
})
}))
})