feat: support dial only on transport manager to tolerate errors (#643)

* feat: support dial only on transport manager to tolerate errors

* chore: address review

* chore: add jsdoc to transport manager tolerance errors
This commit is contained in:
Vasco Santos
2020-05-25 16:49:04 +02:00
committed by Jacob Heun
parent 7f4662f8d8
commit 698c1df1b4
6 changed files with 109 additions and 3 deletions

View File

@ -3,6 +3,8 @@
const mergeOptions = require('merge-options')
const Constants = require('./constants')
const { FaultTolerance } = require('./transport-manager')
const DefaultConfig = {
addresses: {
listen: [],
@ -12,6 +14,9 @@ const DefaultConfig = {
connectionManager: {
minPeers: 25
},
transportManager: {
faultTolerance: FaultTolerance.FATAL_ALL
},
dialer: {
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,

View File

@ -101,7 +101,8 @@ class Libp2p extends EventEmitter {
// Setup the transport manager
this.transportManager = new TransportManager({
libp2p: this,
upgrader: this.upgrader
upgrader: this.upgrader,
faultTolerance: this._options.transportManager.faultTolerance
})
// Create the Registrar

View File

@ -13,12 +13,14 @@ class TransportManager {
* @param {object} options
* @param {Libp2p} options.libp2p The Libp2p instance. It will be passed to the transports.
* @param {Upgrader} options.upgrader The upgrader to provide to the transports
* @param {boolean} [options.faultTolerance = FAULT_TOLERANCE.FATAL_ALL] Address listen error tolerance.
*/
constructor ({ libp2p, upgrader }) {
constructor ({ libp2p, upgrader, faultTolerance = FAULT_TOLERANCE.FATAL_ALL }) {
this.libp2p = libp2p
this.upgrader = upgrader
this._transports = new Map()
this._listeners = new Map()
this.faultTolerance = faultTolerance
}
/**
@ -173,7 +175,11 @@ class TransportManager {
// If no transports were able to listen, throw an error. This likely
// means we were given addresses we do not have transports for
if (couldNotListen.length === this._transports.size) {
throw errCode(new Error(`no valid addresses were provided for transports [${couldNotListen}]`), codes.ERR_NO_VALID_ADDRESSES)
const message = `no valid addresses were provided for transports [${couldNotListen}]`
if (this.faultTolerance === FAULT_TOLERANCE.FATAL_ALL) {
throw errCode(new Error(message), codes.ERR_NO_VALID_ADDRESSES)
}
log(`libp2p in dial mode only: ${message}`)
}
}
@ -212,4 +218,18 @@ class TransportManager {
}
}
/**
* Enum Transport Manager Fault Tolerance values.
* FATAL_ALL should be used for failing in any listen circumstance.
* NO_FATAL should be used for not failing when not listening.
* @readonly
* @enum {number}
*/
const FAULT_TOLERANCE = {
FATAL_ALL: 0,
NO_FATAL: 1
}
TransportManager.FaultTolerance = FAULT_TOLERANCE
module.exports = TransportManager