From 5d460e8815a8b49915da7ffabccc4a8b96a61acc Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Tue, 4 Oct 2022 16:20:52 +0100 Subject: [PATCH] feat: add acceptIncomingConnection to ConnectionManager (#295) * 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 --- packages/interface-connection-manager/src/index.ts | 10 +++++++++- packages/interface-mocks/src/connection-manager.ts | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/interface-connection-manager/src/index.ts b/packages/interface-connection-manager/src/index.ts index 0f34d583e..0eadaebb0 100644 --- a/packages/interface-connection-manager/src/index.ts +++ b/packages/interface-connection-manager/src/index.ts @@ -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' @@ -24,6 +24,14 @@ export interface ConnectionManager extends EventEmitter * Close our connections to a peer */ closeConnections: (peer: PeerId) => Promise + + /** + * 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 } export interface Dialer { diff --git a/packages/interface-mocks/src/connection-manager.ts b/packages/interface-mocks/src/connection-manager.ts index 12e9ee05a..32eeb41f1 100644 --- a/packages/interface-mocks/src/connection-manager.ts +++ b/packages/interface-mocks/src/connection-manager.ts @@ -134,6 +134,10 @@ class MockConnectionManager extends EventEmitter implem await componentsB.getConnectionManager().closeConnections(this.components.getPeerId()) } + + async acceptIncomingConnection (): Promise { + return true + } } export function mockConnectionManager () {