Skip to content

Commit

Permalink
feat: Allow for alternative connection manager (libp2p#1581)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Feb 22, 2023
1 parent 45fc415 commit d1c1b80
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import type { KeyChainInit } from '@libp2p/keychain'
import type { NatManagerInit } from './nat-manager.js'
import type { AddressManagerInit } from './address-manager/index.js'
import type { PeerRoutingInit } from './peer-routing.js'
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
import type { ConnectionManagerInit } from './connection-manager/index.js'

/**
Expand Down Expand Up @@ -149,6 +150,11 @@ export interface Libp2pInit {
* A ConnectionProtector can be used to create a secure overlay on top of the network using pre-shared keys
*/
connectionProtector?: (components: Components) => ConnectionProtector

/**
* Pass a custom ConnectionManager implementation to enable custom connection management
*/
customConnectionManager?: (components: Components) => ConnectionManager
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
this.components.dialer = new DefaultDialer(this.components, init.connectionManager)

// Create the Connection Manager
this.connectionManager = this.components.connectionManager = new DefaultConnectionManager(this.components, init.connectionManager)
// if a connection manager is not provided, create a default one
this.connectionManager = (init.customConnectionManager != null) ? init.customConnectionManager(this.components) : this.components.connectionManager = new DefaultConnectionManager(this.components, init.connectionManager)

// forward connection manager events
this.components.connectionManager.addEventListener('peer:disconnect', (event) => {
Expand Down

0 comments on commit d1c1b80

Please sign in to comment.