diff --git a/src/circuit/auto-relay.js b/src/circuit/auto-relay.js index 8ead2bb5f8..e173ac039b 100644 --- a/src/circuit/auto-relay.js +++ b/src/circuit/auto-relay.js @@ -161,17 +161,24 @@ class AutoRelay { 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 - 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) + } + + return false + }) + ) - // Announce multiaddrs will update on listen success by TransportManager event being triggered + if (result.includes(true)) { + this._listenRelays.add(id) + } } catch (/** @type {any} */ err) { this._onError(err) this._listenRelays.delete(id) diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index 553455fd3e..fa2cb40225 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -112,6 +112,9 @@ class ConnectionManager extends EventEmitter { latencyCheckIntervalMs: this._options.pollInterval, dataEmitIntervalMs: this._options.pollInterval }) + + // This emitter gets listened to a lot + this.setMaxListeners(Infinity) } /** diff --git a/test/relay/auto-relay.node.js b/test/relay/auto-relay.node.js index c9d230a410..2316563dfd 100644 --- a/test/relay/auto-relay.node.js +++ b/test/relay/auto-relay.node.js @@ -224,7 +224,7 @@ describe('auto-relay', () => { 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 sinon.spy(relayLibp2p1.identifyService, 'pushToPeerStore') @@ -236,9 +236,6 @@ describe('auto-relay', () => { // Wait for listening on the relay 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 await relayLibp2p1.hangUp(relayLibp2p2.peerId) @@ -246,9 +243,6 @@ describe('auto-relay', () => { await expect(usingAsRelay(relayLibp2p1, relayLibp2p2, { timeout: 1000 })).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 () => { @@ -271,6 +265,11 @@ describe('auto-relay', () => { // Disconnect from peer used for relay 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 await usingAsRelay(relayLibp2p1, relayLibp2p3) })