From 78e09ff2cf1c5479f9b7b6c9b84800b02bfb7a56 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Fri, 12 Jul 2024 11:48:31 +0300 Subject: [PATCH] add min/max no of connections --- env.md | 3 ++- src/@types/OceanNode.ts | 2 ++ src/components/P2P/index.ts | 4 +++- src/utils/config.ts | 4 +++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/env.md b/env.md index b99ce342d..7b3dfc204 100644 --- a/env.md +++ b/env.md @@ -54,7 +54,8 @@ Environmental variables are also tracked in `ENVIRONMENT_VARIABLES` within `src/ - `P2P_ENABLE_CIRCUIT_RELAY_CLIENT`: Enable connections through relay servers. Default: `True` - `P2P_CIRCUIT_RELAYS`: Numbers of relay servers. Default: `1` - `P2P_BOOTSTRAP_NODES` : List of bootstrap nodes. Defults to OPF nodes. Example: ["/dns4/node3.oceanprotocol.com/tcp/9000/p2p/"] -- `P2P_FILTER_ANNOUNCED_ADDRESSES`: CIDR filters to filter announced addresses. Default: []. Example: ["192.168.0.1/27"] +- `P2P_MIN_CONNECTIONS`: The minimum number of connections below which libp2p will start to dial peers from the peer book. Setting this to 0 disables this behaviour. Default: 1 +- `P2P_MAX_CONNECTIONS`: The maximum number of connections libp2p is willing to have before it starts pruning connections to reduce resource usage. Default: 300 ## Additional Nodes (Test Environments) diff --git a/src/@types/OceanNode.ts b/src/@types/OceanNode.ts index 95b80eb05..f05fef319 100644 --- a/src/@types/OceanNode.ts +++ b/src/@types/OceanNode.ts @@ -45,6 +45,8 @@ export interface OceanNodeP2PConfig { enableCircuitRelayClient: boolean circuitRelays: number announcePrivateIp: boolean + minConnections: number + maxConnections: number } export interface OceanNodeConfig { diff --git a/src/components/P2P/index.ts b/src/components/P2P/index.ts index 87dd319ad..c8d7ea94e 100644 --- a/src/components/P2P/index.ts +++ b/src/components/P2P/index.ts @@ -328,7 +328,9 @@ export class OceanP2P extends EventEmitter { services: servicesConfig, connectionManager: { maxParallelDials: config.p2pConfig.connectionsMaxParallelDials, // 150 total parallel multiaddr dials - dialTimeout: config.p2pConfig.connectionsDialTimeout // 10 second dial timeout per peer dial + dialTimeout: config.p2pConfig.connectionsDialTimeout, // 10 second dial timeout per peer dial + minConnections: config.p2pConfig.minConnections, + maxConnections: config.p2pConfig.maxConnections } } if (config.p2pConfig.bootstrapNodes && config.p2pConfig.bootstrapNodes.length > 0) { diff --git a/src/utils/config.ts b/src/utils/config.ts index c05be43af..d6ebf127b 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -484,7 +484,9 @@ async function getEnvConfig(isStartup?: boolean): Promise { filterAnnouncedAddresses: readListFromEnvVariable( ENVIRONMENT_VARIABLES.P2P_FILTER_ANNOUNCED_ADDRESSES, isStartup - ) + ), + minConnections: getIntEnvValue(process.env.P2P_MIN_CONNECTIONS, 1), + maxConnections: getIntEnvValue(process.env.P2P_MAX_CONNECTIONS, 300) }, // Only enable provider if we have a DB_URL hasProvider: !!getEnvValue(process.env.DB_URL, ''),