libp2p-hs-0.1.0.0: Haskell implementation of the libp2p networking stack
Safe HaskellNone
LanguageGHC2021

LibP2P.Switch.ConnPool

Description

STM-based connection pool for the Switch.

Tracks active connections per peer. All operations are STM-safe for concurrent access from dial, listen, and cleanup threads.

Connection identity uses TVar pointer equality (connState field) since Connection contains function fields that prevent deriving Eq.

Synopsis

Documentation

newConnPool :: IO (TVar (Map PeerId [Connection])) Source #

Create a new empty connection pool.

lookupConn :: TVar (Map PeerId [Connection]) -> PeerId -> STM (Maybe Connection) Source #

Look up the best Open connection for a peer, preferring a direct connection over a relayed one.

specsrelayDCUtR: after a hole punch "the peers should migrate to the established connection by prioritizing over the existing relay connection. All new streams should be opened in the direct connection." Keeping that preference here means every caller migrates at once, the way go-libp2p concentrates it in bestConnToPeer ("If one is limited and not the other, prefer the unlimited connection").

A relayed connection is still returned when it is all there is, so a failed hole punch leaves the relay usable as before.

Returns Nothing if no connection exists or none are in ConnOpen state.

lookupAllConns :: TVar (Map PeerId [Connection]) -> PeerId -> STM [Connection] Source #

Look up all connections for a peer (any state).

addConn :: TVar (Map PeerId [Connection]) -> Connection -> STM () Source #

Add a connection to the pool, keyed by its peer ID.

removeConn :: TVar (Map PeerId [Connection]) -> Connection -> STM () Source #

Remove a specific connection from the pool. Uses TVar reference equality (connState) to identify the connection. Removes empty entries from the map to prevent memory leaks.

allConns :: TVar (Map PeerId [Connection]) -> STM [Connection] Source #

Get all connections across all peers.