Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
feat: add acceptIncomingConnection to ConnectionManager (#295)
Browse files Browse the repository at this point in the history
* feat: add acceptIncomingConnection to ConnectionManager

In order to check we can accept an incoming connection before exchanging
PeerIds, add a method to the ConnectionManager interface that can
be called by the upgrader.

* chore: return boolean
  • Loading branch information
achingbrain authored Oct 4, 2022
1 parent 77e6c89 commit 5d460e8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/interface-connection-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AbortOptions } from '@libp2p/interfaces'
import type { EventEmitter } from '@libp2p/interfaces/events'
import type { Connection } from '@libp2p/interface-connection'
import type { Connection, MultiaddrConnection } from '@libp2p/interface-connection'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { Multiaddr } from '@multiformats/multiaddr'

Expand All @@ -24,6 +24,14 @@ export interface ConnectionManager extends EventEmitter<ConnectionManagerEvents>
* Close our connections to a peer
*/
closeConnections: (peer: PeerId) => Promise<void>

/**
* Invoked after an incoming connection is opened but before PeerIds are
* exchanged, this lets the ConnectionManager check we have sufficient
* resources to accept the connection in which case it will return true,
* otherwise it will return false.
*/
acceptIncomingConnection: (maConn: MultiaddrConnection) => Promise<boolean>
}

export interface Dialer {
Expand Down
4 changes: 4 additions & 0 deletions packages/interface-mocks/src/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem

await componentsB.getConnectionManager().closeConnections(this.components.getPeerId())
}

async acceptIncomingConnection (): Promise<boolean> {
return true
}
}

export function mockConnectionManager () {
Expand Down

0 comments on commit 5d460e8

Please sign in to comment.