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

Network.LibP2P.Switch.ResourceManager

Description

Hierarchical resource management for the Switch (docs/08-switch.md §Resource Limits).

Implements a scope tree (System → Peer → Connection → Stream) with STM-based reserve/release that atomically walks leaf→root to enforce per-scope limits. If any scope exceeds its limit, the entire transaction rolls back.

Synopsis

Direction

data Direction Source #

Direction of a connection relative to this node. Defined here to avoid circular dependency with Switch.Types.

Constructors

Inbound

Remote peer initiated the connection

Outbound

Local node initiated the connection

Instances

Instances details
Show Direction Source # 
Instance details

Defined in Network.LibP2P.Switch.ResourceManager

Eq Direction Source # 
Instance details

Defined in Network.LibP2P.Switch.ResourceManager

Resource tracking

data ResourceUsage Source #

Tracked resource usage at a single scope.

Limits configuration

noLimits :: ResourceLimits Source #

No limits (all Nothing).

data DefaultLimits Source #

Default limits for auto-created scopes.

defaultSystemLimits :: ResourceLimits Source #

Sensible default system limits.

defaultPeerLimits :: ResourceLimits Source #

Sensible default per-peer limits.

Scope hierarchy

data ResourceScope Source #

A node in the scope hierarchy.

data ScopeName Source #

Scope name for debugging and identification.

Instances

Instances details
Show ScopeName Source # 
Instance details

Defined in Network.LibP2P.Switch.ResourceManager

Eq ScopeName Source # 
Instance details

Defined in Network.LibP2P.Switch.ResourceManager

data ResourceError Source #

Resource limit violation error.

Resource manager

newResourceManager :: DefaultLimits -> IO ResourceManager Source #

Create a new resource manager with the given default limits.

Scope management

getOrCreatePeerScope :: ResourceManager -> PeerId -> STM ResourceScope Source #

Get or create a peer scope under the system scope.

Reserve / release

reserveConnection :: ResourceManager -> PeerId -> Direction -> STM (Either ResourceError ()) Source #

Reserve a connection in the hierarchy (peer scope → system scope). Returns Left on limit violation (STM auto-rolls back all increments).

releaseConnection :: ResourceManager -> PeerId -> Direction -> STM () Source #

Release a connection in the hierarchy (peer scope → system scope).

reserveStream :: ResourceScope -> Direction -> STM (Either ResourceError ()) Source #

Reserve a stream in the given scope (walks up to parent).

releaseStream :: ResourceScope -> Direction -> STM () Source #

Release a stream in the given scope (walks up to parent).

Bracket patterns

withConnection :: ResourceManager -> PeerId -> Direction -> IO a -> IO a Source #

Bracket: reserve connection, run action, release on exit (even on exception).

withStream :: ResourceScope -> Direction -> IO a -> IO a Source #

Bracket: reserve stream, run action, release on exit (even on exception).