mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-14 01:31:22 +00:00
fix: try all peer addresses when dialing a relay (#1140)
The order of the addresses can affect our success rate in dialing a relay - if it's a loopback address or similar it won't work. Instead try dialing every address.
This commit is contained in:
@ -161,17 +161,24 @@ class AutoRelay {
|
|||||||
connection.remotePeer, this._addressSorter
|
connection.remotePeer, this._addressSorter
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!remoteAddrs || !remoteAddrs.length) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const listenAddr = `${remoteAddrs[0].toString()}/p2p-circuit`
|
|
||||||
this._listenRelays.add(id)
|
|
||||||
|
|
||||||
// Attempt to listen on relay
|
// Attempt to listen on relay
|
||||||
await this._transportManager.listen([new Multiaddr(listenAddr)])
|
const result = await Promise.all(
|
||||||
|
remoteAddrs.map(async addr => {
|
||||||
|
try {
|
||||||
|
// Announce multiaddrs will update on listen success by TransportManager event being triggered
|
||||||
|
await this._transportManager.listen([new Multiaddr(`${addr.toString()}/p2p-circuit`)])
|
||||||
|
return true
|
||||||
|
} catch (/** @type {any} */ err) {
|
||||||
|
this._onError(err)
|
||||||
|
}
|
||||||
|
|
||||||
// Announce multiaddrs will update on listen success by TransportManager event being triggered
|
return false
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
if (result.includes(true)) {
|
||||||
|
this._listenRelays.add(id)
|
||||||
|
}
|
||||||
} catch (/** @type {any} */ err) {
|
} catch (/** @type {any} */ err) {
|
||||||
this._onError(err)
|
this._onError(err)
|
||||||
this._listenRelays.delete(id)
|
this._listenRelays.delete(id)
|
||||||
|
@ -112,6 +112,9 @@ class ConnectionManager extends EventEmitter {
|
|||||||
latencyCheckIntervalMs: this._options.pollInterval,
|
latencyCheckIntervalMs: this._options.pollInterval,
|
||||||
dataEmitIntervalMs: this._options.pollInterval
|
dataEmitIntervalMs: this._options.pollInterval
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// This emitter gets listened to a lot
|
||||||
|
this.setMaxListeners(Infinity)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -224,7 +224,7 @@ describe('auto-relay', () => {
|
|||||||
expect(knownProtocols3).to.include(relayMulticodec)
|
expect(knownProtocols3).to.include(relayMulticodec)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not listen on a relayed address if peer disconnects', async () => {
|
it('should not listen on a relayed address we disconnect from peer', async () => {
|
||||||
// Spy if identify push is fired on adding/removing listen addr
|
// Spy if identify push is fired on adding/removing listen addr
|
||||||
sinon.spy(relayLibp2p1.identifyService, 'pushToPeerStore')
|
sinon.spy(relayLibp2p1.identifyService, 'pushToPeerStore')
|
||||||
|
|
||||||
@ -236,9 +236,6 @@ describe('auto-relay', () => {
|
|||||||
// Wait for listening on the relay
|
// Wait for listening on the relay
|
||||||
await usingAsRelay(relayLibp2p1, relayLibp2p2)
|
await usingAsRelay(relayLibp2p1, relayLibp2p2)
|
||||||
|
|
||||||
// Identify push for adding listen relay multiaddr
|
|
||||||
expect(relayLibp2p1.identifyService.pushToPeerStore.callCount).to.equal(1)
|
|
||||||
|
|
||||||
// Disconnect from peer used for relay
|
// Disconnect from peer used for relay
|
||||||
await relayLibp2p1.hangUp(relayLibp2p2.peerId)
|
await relayLibp2p1.hangUp(relayLibp2p2.peerId)
|
||||||
|
|
||||||
@ -246,9 +243,6 @@ describe('auto-relay', () => {
|
|||||||
await expect(usingAsRelay(relayLibp2p1, relayLibp2p2, {
|
await expect(usingAsRelay(relayLibp2p1, relayLibp2p2, {
|
||||||
timeout: 1000
|
timeout: 1000
|
||||||
})).to.eventually.be.rejected()
|
})).to.eventually.be.rejected()
|
||||||
|
|
||||||
// Identify push for removing listen relay multiaddr
|
|
||||||
await pWaitFor(() => relayLibp2p1.identifyService.pushToPeerStore.callCount === 2)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should try to listen on other connected peers relayed address if one used relay disconnects', async () => {
|
it('should try to listen on other connected peers relayed address if one used relay disconnects', async () => {
|
||||||
@ -271,6 +265,11 @@ describe('auto-relay', () => {
|
|||||||
// Disconnect from peer used for relay
|
// Disconnect from peer used for relay
|
||||||
await relayLibp2p2.stop()
|
await relayLibp2p2.stop()
|
||||||
|
|
||||||
|
// Should not be using the relay any more
|
||||||
|
await expect(usingAsRelay(relayLibp2p1, relayLibp2p2, {
|
||||||
|
timeout: 1000
|
||||||
|
})).to.eventually.be.rejected()
|
||||||
|
|
||||||
// Wait for other peer connected to be added as listen addr
|
// Wait for other peer connected to be added as listen addr
|
||||||
await usingAsRelay(relayLibp2p1, relayLibp2p3)
|
await usingAsRelay(relayLibp2p1, relayLibp2p3)
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user