mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-23 22:11:35 +00:00
chore: refactor connection manager and registrar
This commit is contained in:
65
src/index.js
65
src/index.js
@ -54,55 +54,40 @@ class Libp2p extends EventEmitter {
|
||||
this._transport = [] // Transport instances/references
|
||||
this._discovery = new Map() // Discovery service instances/references
|
||||
|
||||
// Create the Connection Manager
|
||||
this.connectionManager = new ConnectionManager(this, this._options.connectionManager)
|
||||
|
||||
// Create Metrics
|
||||
if (this._options.metrics.enabled) {
|
||||
this.metrics = new Metrics(this._options.metrics)
|
||||
this.metrics = new Metrics({
|
||||
...this._options.metrics,
|
||||
connectionManager: this.connectionManager
|
||||
})
|
||||
}
|
||||
|
||||
// Setup the Upgrader
|
||||
this.upgrader = new Upgrader({
|
||||
localPeer: this.peerId,
|
||||
metrics: this.metrics,
|
||||
onConnection: (connection) => {
|
||||
const peerId = connection.remotePeer
|
||||
|
||||
this.registrar.onConnect(peerId, connection)
|
||||
this.connectionManager.onConnect(connection)
|
||||
this.emit('peer:connect', peerId)
|
||||
|
||||
// Run identify for every connection
|
||||
if (this.identifyService) {
|
||||
this.identifyService.identify(connection, peerId)
|
||||
.catch(log.error)
|
||||
}
|
||||
},
|
||||
onConnectionEnd: (connection) => {
|
||||
const peerId = connection.remotePeer
|
||||
|
||||
this.registrar.onDisconnect(peerId, connection)
|
||||
this.connectionManager.onDisconnect(connection)
|
||||
|
||||
// If there are no connections to the peer, disconnect
|
||||
if (!this.registrar.getConnection(peerId)) {
|
||||
this.emit('peer:disconnect', peerId)
|
||||
this.metrics && this.metrics.onPeerDisconnected(peerId)
|
||||
}
|
||||
}
|
||||
onConnection: (connection) => this.connectionManager.onConnect(connection),
|
||||
onConnectionEnd: (connection) => this.connectionManager.onDisconnect(connection)
|
||||
})
|
||||
|
||||
// Create the Registrar
|
||||
this.registrar = new Registrar({ peerStore: this.peerStore })
|
||||
this.handle = this.handle.bind(this)
|
||||
this.registrar.handle = this.handle
|
||||
|
||||
// Create the Connection Manager
|
||||
this.connectionManager = new ConnectionManager(this, this._options.connectionManager)
|
||||
|
||||
// Setup the transport manager
|
||||
this.transportManager = new TransportManager({
|
||||
libp2p: this,
|
||||
upgrader: this.upgrader
|
||||
})
|
||||
|
||||
// Create the Registrar
|
||||
this.registrar = new Registrar({
|
||||
peerStore: this.peerStore,
|
||||
connectionManager: this.connectionManager
|
||||
})
|
||||
|
||||
this.handle = this.handle.bind(this)
|
||||
this.registrar.handle = this.handle
|
||||
|
||||
// Attach crypto channels
|
||||
if (this._modules.connEncryption) {
|
||||
const cryptos = this._modules.connEncryption
|
||||
@ -138,7 +123,8 @@ class Libp2p extends EventEmitter {
|
||||
|
||||
// Add the identify service since we can multiplex
|
||||
this.identifyService = new IdentifyService({
|
||||
registrar: this.registrar,
|
||||
peerStore: this.peerStore,
|
||||
connectionManager: this.connectionManager,
|
||||
peerId: this.peerId,
|
||||
addresses: this.addresses,
|
||||
protocols: this.upgrader.protocols
|
||||
@ -242,7 +228,6 @@ class Libp2p extends EventEmitter {
|
||||
])
|
||||
|
||||
await this.transportManager.close()
|
||||
await this.registrar.close()
|
||||
|
||||
ping.unmount(this)
|
||||
this.dialer.destroy()
|
||||
@ -294,7 +279,7 @@ class Libp2p extends EventEmitter {
|
||||
*/
|
||||
async dialProtocol (peer, protocols, options) {
|
||||
const { id, multiaddrs } = getPeer(peer, this.peerStore)
|
||||
let connection = this.registrar.getConnection(id)
|
||||
let connection = this.connectionManager.get(id)
|
||||
|
||||
if (!connection) {
|
||||
connection = await this.dialer.connectToPeer(peer, options)
|
||||
@ -318,7 +303,7 @@ class Libp2p extends EventEmitter {
|
||||
async hangUp (peer) {
|
||||
const { id } = getPeer(peer)
|
||||
|
||||
const connections = this.registrar.connections.get(id.toB58String())
|
||||
const connections = this.connectionManager.connections.get(id.toB58String())
|
||||
|
||||
if (!connections) {
|
||||
return
|
||||
@ -452,9 +437,9 @@ class Libp2p extends EventEmitter {
|
||||
*/
|
||||
async _maybeConnect (peerId) {
|
||||
// If auto dialing is on and we have no connection to the peer, check if we should dial
|
||||
if (this._config.peerDiscovery.autoDial === true && !this.registrar.getConnection(peerId)) {
|
||||
if (this._config.peerDiscovery.autoDial === true && !this.connectionManager.get(peerId)) {
|
||||
const minPeers = this._options.connectionManager.minPeers || 0
|
||||
if (minPeers > this.connectionManager._connections.size) {
|
||||
if (minPeers > this.connectionManager.size) {
|
||||
log('connecting to discovered peer %s', peerId.toB58String())
|
||||
try {
|
||||
await this.dialer.connectToPeer(peerId)
|
||||
|
Reference in New Issue
Block a user