Skip to content

Commit

Permalink
fix: not dial all known peers on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jul 9, 2020
1 parent 7b05d69 commit 695f77c
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,7 @@ class Libp2p extends EventEmitter {
})

// Once we start, emit and dial any peers we may have already discovered
for (const peer of this.peerStore.peers.values()) {
this.emit('peer:discovery', peer.id)
this._maybeConnect(peer.id)
}
this._maybeConnectPeerStorePeers()

// Peer discovery
await this._setupPeerDiscovery()
Expand All @@ -492,6 +489,33 @@ class Libp2p extends EventEmitter {
peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols)
}

/**
* Will dial a substet of the peers already known
* @private
* @returns {void}
*/
_maybeConnectPeerStorePeers () {
if (!this._config.peerDiscovery.autoDial) {
return
}

const minPeers = this._options.connectionManager.minPeers || 0

// TODO: this should be ordered by score on PeerStoreV2
this.peerStore.peers.forEach(async (peer) => {
this.emit('peer:discovery', peer.id)

if (minPeers > this.connectionManager.size && !this.connectionManager.get(peer.id)) {
log('connecting to previously discovered peer %s', peer.id.toB58String())
try {
await this.dialer.connectToPeer(peer.id)
} catch (err) {
log.error('could not connect to previously discovered peer', err)
}
}
})
}

/**
* Will dial to the given `peerId` if the current number of
* connected peers is less than the configured `ConnectionManager`
Expand Down

0 comments on commit 695f77c

Please sign in to comment.