| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
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
- data Direction
- data ResourceUsage = ResourceUsage {
- ruConnsInbound :: !Int
- ruConnsOutbound :: !Int
- ruStreamsInbound :: !Int
- ruStreamsOutbound :: !Int
- ruMemory :: !Int
- emptyUsage :: ResourceUsage
- data ResourceLimits = ResourceLimits {
- rlMaxConnsInbound :: !(Maybe Int)
- rlMaxConnsOutbound :: !(Maybe Int)
- rlMaxConnsTotal :: !(Maybe Int)
- rlMaxStreamsInbound :: !(Maybe Int)
- rlMaxStreamsOutbound :: !(Maybe Int)
- rlMaxStreamsTotal :: !(Maybe Int)
- rlMaxMemory :: !(Maybe Int)
- noLimits :: ResourceLimits
- data DefaultLimits = DefaultLimits {}
- defaultSystemLimits :: ResourceLimits
- defaultPeerLimits :: ResourceLimits
- data ResourceScope = ResourceScope {
- rsName :: !ScopeName
- rsUsage :: !(TVar ResourceUsage)
- rsLimits :: !ResourceLimits
- rsParent :: !(Maybe ResourceScope)
- data ScopeName
- data ResourceError = ResourceLimitExceeded !ScopeName !String
- data ResourceManager = ResourceManager {
- rmSystemScope :: !ResourceScope
- rmDefaults :: !DefaultLimits
- rmPeerScopes :: !(TVar (Map PeerId ResourceScope))
- newResourceManager :: DefaultLimits -> IO ResourceManager
- getOrCreatePeerScope :: ResourceManager -> PeerId -> STM ResourceScope
- reserveConnection :: ResourceManager -> PeerId -> Direction -> STM (Either ResourceError ())
- releaseConnection :: ResourceManager -> PeerId -> Direction -> STM ()
- reserveStream :: ResourceScope -> Direction -> STM (Either ResourceError ())
- releaseStream :: ResourceScope -> Direction -> STM ()
- withConnection :: ResourceManager -> PeerId -> Direction -> IO a -> IO a
- withStream :: ResourceScope -> Direction -> IO a -> IO a
Direction
Direction of a connection relative to this node. Defined here to avoid circular dependency with Switch.Types.
Resource tracking
data ResourceUsage Source #
Tracked resource usage at a single scope.
Constructors
| ResourceUsage | |
Fields
| |
Instances
| Show ResourceUsage Source # | |
Defined in Network.LibP2P.Switch.ResourceManager Methods showsPrec :: Int -> ResourceUsage -> ShowS # show :: ResourceUsage -> String # showList :: [ResourceUsage] -> ShowS # | |
| Eq ResourceUsage Source # | |
Defined in Network.LibP2P.Switch.ResourceManager Methods (==) :: ResourceUsage -> ResourceUsage -> Bool # (/=) :: ResourceUsage -> ResourceUsage -> Bool # | |
emptyUsage :: ResourceUsage Source #
Zero usage.
Limits configuration
data ResourceLimits Source #
Configurable limits per scope. Nothing = unlimited.
Constructors
| ResourceLimits | |
Fields
| |
Instances
| Show ResourceLimits Source # | |
Defined in Network.LibP2P.Switch.ResourceManager Methods showsPrec :: Int -> ResourceLimits -> ShowS # show :: ResourceLimits -> String # showList :: [ResourceLimits] -> ShowS # | |
| Eq ResourceLimits Source # | |
Defined in Network.LibP2P.Switch.ResourceManager Methods (==) :: ResourceLimits -> ResourceLimits -> Bool # (/=) :: ResourceLimits -> ResourceLimits -> Bool # | |
noLimits :: ResourceLimits Source #
No limits (all Nothing).
data DefaultLimits Source #
Default limits for auto-created scopes.
Constructors
| DefaultLimits | |
Fields | |
Instances
| Show DefaultLimits Source # | |
Defined in Network.LibP2P.Switch.ResourceManager Methods showsPrec :: Int -> DefaultLimits -> ShowS # show :: DefaultLimits -> String # showList :: [DefaultLimits] -> ShowS # | |
| Eq DefaultLimits Source # | |
Defined in Network.LibP2P.Switch.ResourceManager Methods (==) :: DefaultLimits -> DefaultLimits -> Bool # (/=) :: DefaultLimits -> DefaultLimits -> Bool # | |
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.
Constructors
| ResourceScope | |
Fields
| |
Scope name for debugging and identification.
Constructors
| SystemScope | |
| PeerScope !PeerId | |
| ConnectionScope | |
| StreamScope |
data ResourceError Source #
Resource limit violation error.
Constructors
| ResourceLimitExceeded !ScopeName !String |
Instances
| Show ResourceError Source # | |
Defined in Network.LibP2P.Switch.ResourceManager Methods showsPrec :: Int -> ResourceError -> ShowS # show :: ResourceError -> String # showList :: [ResourceError] -> ShowS # | |
| Eq ResourceError Source # | |
Defined in Network.LibP2P.Switch.ResourceManager Methods (==) :: ResourceError -> ResourceError -> Bool # (/=) :: ResourceError -> ResourceError -> Bool # | |
Resource manager
data ResourceManager Source #
Top-level resource manager.
Constructors
| ResourceManager | |
Fields
| |
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).