feat!: re-architect connection manager (#2445)
* remove public pubsub field and redundant util
* add hangUp and improve dial operations, improve keepAliveManager and remove unused method, move utils and add tests
* improve public dial method to start keep alive checks
* move dial method
* implement discovery dialer
* implement discovery dialer with queue with tests
* add discovery dialer e2e tests, change local discovery log tag, update other tests
* remove comment
* add issue link, remove only
* implement shard reader component
* create evetns module, remove unused connection manager events and related tests
* implement network indicator
* implement connection limiter, change public API of connection manager, implement recovery strategy
* decouple keep alive maanger
* add connection manager js-doc
* refactor keep alive manager, cover with tests
* add tests for connection manager main facade
* add tests for connection limiter
* add e2e tests for connection manager modules
pass js-waku config during test node init
remove dns discovery for js-waku
* restructure dialing tests
* address last e2e tests
* address review
* add logging for main methods
* decouple pure dialer class, update network monitor with specific metrics
* remove console.log
* remove usage of protocols
* update sdk package tests
* add connect await promise
* add debug for e2e tests
* enable only packages tests
* use only one file
* revert debugging
* up interface for netwrok manager
* add logs
* add more logs
* add more logs
* add another logs
* remove .only
* remove log statements
* skip the test with follow up
2025-07-09 21:23:14 +02:00
|
|
|
import type { Peer, PeerId, Stream } from "@libp2p/interface";
|
|
|
|
|
import type { MultiaddrInput } from "@multiformats/multiaddr";
|
2023-07-26 22:51:55 +05:30
|
|
|
|
2025-07-17 01:15:36 +02:00
|
|
|
// Peer tags
|
2023-02-20 15:30:59 +05:30
|
|
|
export enum Tags {
|
|
|
|
|
BOOTSTRAP = "bootstrap",
|
2024-02-16 20:06:27 +05:30
|
|
|
PEER_EXCHANGE = "peer-exchange",
|
2024-02-29 01:05:54 +05:30
|
|
|
LOCAL = "local-peer-cache"
|
2023-02-20 15:30:59 +05:30
|
|
|
}
|
|
|
|
|
|
2025-07-17 01:15:36 +02:00
|
|
|
// Connection tag
|
|
|
|
|
export const CONNECTION_LOCKED_TAG = "locked";
|
|
|
|
|
|
2025-01-31 00:16:00 +01:00
|
|
|
export type ConnectionManagerOptions = {
|
2023-02-20 15:30:59 +05:30
|
|
|
/**
|
2025-01-31 00:16:00 +01:00
|
|
|
* Max number of bootstrap peers allowed to be connected to initially.
|
|
|
|
|
* This is used to increase intention of dialing non-bootstrap peers, found using other discovery mechanisms (like Peer Exchange).
|
|
|
|
|
*
|
2025-07-17 01:15:36 +02:00
|
|
|
* @default 3
|
2023-02-20 15:30:59 +05:30
|
|
|
*/
|
feat!: re-architect connection manager (#2445)
* remove public pubsub field and redundant util
* add hangUp and improve dial operations, improve keepAliveManager and remove unused method, move utils and add tests
* improve public dial method to start keep alive checks
* move dial method
* implement discovery dialer
* implement discovery dialer with queue with tests
* add discovery dialer e2e tests, change local discovery log tag, update other tests
* remove comment
* add issue link, remove only
* implement shard reader component
* create evetns module, remove unused connection manager events and related tests
* implement network indicator
* implement connection limiter, change public API of connection manager, implement recovery strategy
* decouple keep alive maanger
* add connection manager js-doc
* refactor keep alive manager, cover with tests
* add tests for connection manager main facade
* add tests for connection limiter
* add e2e tests for connection manager modules
pass js-waku config during test node init
remove dns discovery for js-waku
* restructure dialing tests
* address last e2e tests
* address review
* add logging for main methods
* decouple pure dialer class, update network monitor with specific metrics
* remove console.log
* remove usage of protocols
* update sdk package tests
* add connect await promise
* add debug for e2e tests
* enable only packages tests
* use only one file
* revert debugging
* up interface for netwrok manager
* add logs
* add more logs
* add more logs
* add another logs
* remove .only
* remove log statements
* skip the test with follow up
2025-07-09 21:23:14 +02:00
|
|
|
maxBootstrapPeers: number;
|
2025-01-31 00:16:00 +01:00
|
|
|
|
2025-07-17 01:15:36 +02:00
|
|
|
/**
|
|
|
|
|
* Max number of connections allowed to be connected to.
|
|
|
|
|
*
|
|
|
|
|
* @default 10
|
|
|
|
|
*/
|
|
|
|
|
maxConnections: number;
|
|
|
|
|
|
2025-01-31 00:16:00 +01:00
|
|
|
/**
|
|
|
|
|
* Keep alive libp2p pings interval in seconds.
|
|
|
|
|
*
|
|
|
|
|
* @default 300 seconds
|
|
|
|
|
*/
|
|
|
|
|
pingKeepAlive: number;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gossip sub specific keep alive interval in seconds.
|
|
|
|
|
*
|
|
|
|
|
* @default 300 seconds
|
|
|
|
|
*/
|
|
|
|
|
relayKeepAlive: number;
|
2025-07-15 23:13:58 +02:00
|
|
|
|
2025-07-17 01:15:36 +02:00
|
|
|
/**
|
|
|
|
|
* Enable auto recovery of connections if has not enough:
|
|
|
|
|
* - bootstrap peers
|
|
|
|
|
* - LightPush and Filter peers
|
|
|
|
|
* - number of connected peers
|
|
|
|
|
* - dial known peers on reconnect to Internet
|
|
|
|
|
*
|
|
|
|
|
* @default true
|
|
|
|
|
*/
|
|
|
|
|
enableAutoRecovery: boolean;
|
|
|
|
|
|
2025-07-15 23:13:58 +02:00
|
|
|
/**
|
|
|
|
|
* Max number of peers to dial at once.
|
|
|
|
|
*
|
|
|
|
|
* @default 3
|
|
|
|
|
*/
|
|
|
|
|
maxDialingPeers: number;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Time to wait before dialing failed peers again.
|
|
|
|
|
*
|
|
|
|
|
* @default 60 seconds
|
|
|
|
|
*/
|
|
|
|
|
failedDialCooldown: number;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Time to wait before dialing a peer again.
|
|
|
|
|
*
|
|
|
|
|
* @default 10 seconds
|
|
|
|
|
*/
|
|
|
|
|
dialCooldown: number;
|
2025-01-31 00:16:00 +01:00
|
|
|
};
|
2023-07-26 22:51:55 +05:30
|
|
|
|
feat!: re-architect connection manager (#2445)
* remove public pubsub field and redundant util
* add hangUp and improve dial operations, improve keepAliveManager and remove unused method, move utils and add tests
* improve public dial method to start keep alive checks
* move dial method
* implement discovery dialer
* implement discovery dialer with queue with tests
* add discovery dialer e2e tests, change local discovery log tag, update other tests
* remove comment
* add issue link, remove only
* implement shard reader component
* create evetns module, remove unused connection manager events and related tests
* implement network indicator
* implement connection limiter, change public API of connection manager, implement recovery strategy
* decouple keep alive maanger
* add connection manager js-doc
* refactor keep alive manager, cover with tests
* add tests for connection manager main facade
* add tests for connection limiter
* add e2e tests for connection manager modules
pass js-waku config during test node init
remove dns discovery for js-waku
* restructure dialing tests
* address last e2e tests
* address review
* add logging for main methods
* decouple pure dialer class, update network monitor with specific metrics
* remove console.log
* remove usage of protocols
* update sdk package tests
* add connect await promise
* add debug for e2e tests
* enable only packages tests
* use only one file
* revert debugging
* up interface for netwrok manager
* add logs
* add more logs
* add more logs
* add another logs
* remove .only
* remove log statements
* skip the test with follow up
2025-07-09 21:23:14 +02:00
|
|
|
export interface IConnectionManager {
|
|
|
|
|
/**
|
|
|
|
|
* Starts network monitoring, dialing discovered peers, keep-alive management, and connection limiting.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```typescript
|
|
|
|
|
* const connectionManager = new ConnectionManager(options);
|
|
|
|
|
* connectionManager.start();
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
start(): void;
|
2023-07-26 22:51:55 +05:30
|
|
|
|
feat!: re-architect connection manager (#2445)
* remove public pubsub field and redundant util
* add hangUp and improve dial operations, improve keepAliveManager and remove unused method, move utils and add tests
* improve public dial method to start keep alive checks
* move dial method
* implement discovery dialer
* implement discovery dialer with queue with tests
* add discovery dialer e2e tests, change local discovery log tag, update other tests
* remove comment
* add issue link, remove only
* implement shard reader component
* create evetns module, remove unused connection manager events and related tests
* implement network indicator
* implement connection limiter, change public API of connection manager, implement recovery strategy
* decouple keep alive maanger
* add connection manager js-doc
* refactor keep alive manager, cover with tests
* add tests for connection manager main facade
* add tests for connection limiter
* add e2e tests for connection manager modules
pass js-waku config during test node init
remove dns discovery for js-waku
* restructure dialing tests
* address last e2e tests
* address review
* add logging for main methods
* decouple pure dialer class, update network monitor with specific metrics
* remove console.log
* remove usage of protocols
* update sdk package tests
* add connect await promise
* add debug for e2e tests
* enable only packages tests
* use only one file
* revert debugging
* up interface for netwrok manager
* add logs
* add more logs
* add more logs
* add another logs
* remove .only
* remove log statements
* skip the test with follow up
2025-07-09 21:23:14 +02:00
|
|
|
/**
|
|
|
|
|
* Stops network monitoring, discovery dialing, keep-alive management, and connection limiting.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```typescript
|
|
|
|
|
* connectionManager.stop();
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
stop(): void;
|
2023-07-31 13:54:39 +05:30
|
|
|
|
feat!: re-architect connection manager (#2445)
* remove public pubsub field and redundant util
* add hangUp and improve dial operations, improve keepAliveManager and remove unused method, move utils and add tests
* improve public dial method to start keep alive checks
* move dial method
* implement discovery dialer
* implement discovery dialer with queue with tests
* add discovery dialer e2e tests, change local discovery log tag, update other tests
* remove comment
* add issue link, remove only
* implement shard reader component
* create evetns module, remove unused connection manager events and related tests
* implement network indicator
* implement connection limiter, change public API of connection manager, implement recovery strategy
* decouple keep alive maanger
* add connection manager js-doc
* refactor keep alive manager, cover with tests
* add tests for connection manager main facade
* add tests for connection limiter
* add e2e tests for connection manager modules
pass js-waku config during test node init
remove dns discovery for js-waku
* restructure dialing tests
* address last e2e tests
* address review
* add logging for main methods
* decouple pure dialer class, update network monitor with specific metrics
* remove console.log
* remove usage of protocols
* update sdk package tests
* add connect await promise
* add debug for e2e tests
* enable only packages tests
* use only one file
* revert debugging
* up interface for netwrok manager
* add logs
* add more logs
* add more logs
* add another logs
* remove .only
* remove log statements
* skip the test with follow up
2025-07-09 21:23:14 +02:00
|
|
|
/**
|
|
|
|
|
* Connects to a peer using specific protocol codecs.
|
|
|
|
|
* This is a direct proxy to libp2p's dialProtocol method.
|
|
|
|
|
*
|
|
|
|
|
* @param peer - The peer to connect to (PeerId or multiaddr)
|
|
|
|
|
* @param protocolCodecs - Array of protocol codec strings to establish
|
|
|
|
|
* @returns Promise resolving to a Stream connection to the peer
|
|
|
|
|
* @throws Error if the connection cannot be established
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```typescript
|
|
|
|
|
* const stream = await connectionManager.dial(
|
|
|
|
|
* peerId,
|
|
|
|
|
* ["/vac/waku/store/2.0.0-beta4"]
|
|
|
|
|
* );
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
dial(
|
|
|
|
|
peer: PeerId | MultiaddrInput,
|
|
|
|
|
protocolCodecs: string[]
|
|
|
|
|
): Promise<Stream>;
|
2023-11-27 03:44:49 -08:00
|
|
|
|
feat!: re-architect connection manager (#2445)
* remove public pubsub field and redundant util
* add hangUp and improve dial operations, improve keepAliveManager and remove unused method, move utils and add tests
* improve public dial method to start keep alive checks
* move dial method
* implement discovery dialer
* implement discovery dialer with queue with tests
* add discovery dialer e2e tests, change local discovery log tag, update other tests
* remove comment
* add issue link, remove only
* implement shard reader component
* create evetns module, remove unused connection manager events and related tests
* implement network indicator
* implement connection limiter, change public API of connection manager, implement recovery strategy
* decouple keep alive maanger
* add connection manager js-doc
* refactor keep alive manager, cover with tests
* add tests for connection manager main facade
* add tests for connection limiter
* add e2e tests for connection manager modules
pass js-waku config during test node init
remove dns discovery for js-waku
* restructure dialing tests
* address last e2e tests
* address review
* add logging for main methods
* decouple pure dialer class, update network monitor with specific metrics
* remove console.log
* remove usage of protocols
* update sdk package tests
* add connect await promise
* add debug for e2e tests
* enable only packages tests
* use only one file
* revert debugging
* up interface for netwrok manager
* add logs
* add more logs
* add more logs
* add another logs
* remove .only
* remove log statements
* skip the test with follow up
2025-07-09 21:23:14 +02:00
|
|
|
/**
|
|
|
|
|
* Terminates the connection to a specific peer.
|
|
|
|
|
*
|
|
|
|
|
* @param peer - The peer to disconnect from (PeerId or multiaddr)
|
|
|
|
|
* @returns Promise resolving to true if disconnection was successful, false otherwise
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```typescript
|
|
|
|
|
* const success = await connectionManager.hangUp(peerId);
|
|
|
|
|
* if (success) {
|
|
|
|
|
* console.log("Peer disconnected successfully");
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
hangUp(peer: PeerId | MultiaddrInput): Promise<boolean>;
|
2023-11-27 03:44:49 -08:00
|
|
|
|
feat!: re-architect connection manager (#2445)
* remove public pubsub field and redundant util
* add hangUp and improve dial operations, improve keepAliveManager and remove unused method, move utils and add tests
* improve public dial method to start keep alive checks
* move dial method
* implement discovery dialer
* implement discovery dialer with queue with tests
* add discovery dialer e2e tests, change local discovery log tag, update other tests
* remove comment
* add issue link, remove only
* implement shard reader component
* create evetns module, remove unused connection manager events and related tests
* implement network indicator
* implement connection limiter, change public API of connection manager, implement recovery strategy
* decouple keep alive maanger
* add connection manager js-doc
* refactor keep alive manager, cover with tests
* add tests for connection manager main facade
* add tests for connection limiter
* add e2e tests for connection manager modules
pass js-waku config during test node init
remove dns discovery for js-waku
* restructure dialing tests
* address last e2e tests
* address review
* add logging for main methods
* decouple pure dialer class, update network monitor with specific metrics
* remove console.log
* remove usage of protocols
* update sdk package tests
* add connect await promise
* add debug for e2e tests
* enable only packages tests
* use only one file
* revert debugging
* up interface for netwrok manager
* add logs
* add more logs
* add more logs
* add another logs
* remove .only
* remove log statements
* skip the test with follow up
2025-07-09 21:23:14 +02:00
|
|
|
/**
|
|
|
|
|
* Retrieves a list of currently connected peers, optionally filtered by protocol codec.
|
|
|
|
|
* Results are sorted by ping time (lowest first).
|
|
|
|
|
*
|
|
|
|
|
* @param codec - Optional protocol codec to filter peers by
|
|
|
|
|
* @returns Promise resolving to an array of connected Peer objects
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```typescript
|
|
|
|
|
* // Get all connected peers
|
|
|
|
|
* const allPeers = await connectionManager.getConnectedPeers();
|
|
|
|
|
*
|
|
|
|
|
* // Get peers supporting a specific protocol
|
|
|
|
|
* const storePeers = await connectionManager.getConnectedPeers(
|
|
|
|
|
* "/vac/waku/store/2.0.0-beta4"
|
|
|
|
|
* );
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2025-01-31 00:16:00 +01:00
|
|
|
getConnectedPeers(codec?: string): Promise<Peer[]>;
|
feat!: re-architect connection manager (#2445)
* remove public pubsub field and redundant util
* add hangUp and improve dial operations, improve keepAliveManager and remove unused method, move utils and add tests
* improve public dial method to start keep alive checks
* move dial method
* implement discovery dialer
* implement discovery dialer with queue with tests
* add discovery dialer e2e tests, change local discovery log tag, update other tests
* remove comment
* add issue link, remove only
* implement shard reader component
* create evetns module, remove unused connection manager events and related tests
* implement network indicator
* implement connection limiter, change public API of connection manager, implement recovery strategy
* decouple keep alive maanger
* add connection manager js-doc
* refactor keep alive manager, cover with tests
* add tests for connection manager main facade
* add tests for connection limiter
* add e2e tests for connection manager modules
pass js-waku config during test node init
remove dns discovery for js-waku
* restructure dialing tests
* address last e2e tests
* address review
* add logging for main methods
* decouple pure dialer class, update network monitor with specific metrics
* remove console.log
* remove usage of protocols
* update sdk package tests
* add connect await promise
* add debug for e2e tests
* enable only packages tests
* use only one file
* revert debugging
* up interface for netwrok manager
* add logs
* add more logs
* add more logs
* add another logs
* remove .only
* remove log statements
* skip the test with follow up
2025-07-09 21:23:14 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if a peer has shard info.
|
|
|
|
|
*
|
|
|
|
|
* @param peerId - The peer to check
|
|
|
|
|
* @returns Promise resolving to true if the peer has shard info, false otherwise
|
|
|
|
|
*/
|
|
|
|
|
hasShardInfo(peerId: PeerId): Promise<boolean>;
|
2023-07-31 13:54:39 +05:30
|
|
|
}
|