From e8a09a138386d598d6490cc784316e5cdbbc036a Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 30 Sep 2022 13:38:12 +0100 Subject: [PATCH 1/2] 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. --- 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..55c5fa3e1 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, otherwise it will be closed and an + * error thrown + */ + 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..3e51a5622 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 () { + + } } export function mockConnectionManager () { From ed1e9d1b1816cd8440bd407a2f39d74bfb34f5fc Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 4 Oct 2022 15:11:53 +0100 Subject: [PATCH 2/2] chore: return boolean --- packages/interface-connection-manager/src/index.ts | 6 +++--- packages/interface-mocks/src/connection-manager.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/interface-connection-manager/src/index.ts b/packages/interface-connection-manager/src/index.ts index 55c5fa3e1..0eadaebb0 100644 --- a/packages/interface-connection-manager/src/index.ts +++ b/packages/interface-connection-manager/src/index.ts @@ -28,10 +28,10 @@ export interface ConnectionManager extends EventEmitter /** * 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, otherwise it will be closed and an - * error thrown + * resources to accept the connection in which case it will return true, + * otherwise it will return false. */ - acceptIncomingConnection: (maConn: MultiaddrConnection) => Promise + 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 3e51a5622..32eeb41f1 100644 --- a/packages/interface-mocks/src/connection-manager.ts +++ b/packages/interface-mocks/src/connection-manager.ts @@ -135,8 +135,8 @@ class MockConnectionManager extends EventEmitter implem await componentsB.getConnectionManager().closeConnections(this.components.getPeerId()) } - async acceptIncomingConnection () { - + async acceptIncomingConnection (): Promise { + return true } }