Understand libp2p Resource Scopes
masterThe libp2p Resource Manager uses a Directed Acyclic Graph (DAG) of Resource Scopes to enforce multi-resolution resource accounting. Resource usage is aggregated from leaf nodes (like Streams or Connections) up through intermediate nodes (Peers, Protocols, Services) to the top-level System scope. This allows for granular control, such as limiting the impact of a single peer or a specific protocol without affecting the entire system.
Scope Hierarchy
- System Scope: The top-level scope that enforces global hard limits across the entire system.
- Transient Scope: A 'DMZ' scope for resources in the process of being established (e.g., a connection undergoing a handshake or a stream before protocol negotiation). This prevents attacks targeting unestablished resources.
- Service Scopes: Logical groupings of streams (e.g., DHT, PubSub, or ambient services like Identify) used to isolate and tune resource usage for specific application components.
- Protocol Scopes: Intermediate scopes used to restrict specific protocols. This is useful for limiting legacy or inefficient protocols (e.g., limiting
floodsubwithin agossipsubservice) to protect the application. - Peer Scopes: Constrains resources (connections and streams) used by an individual remote peer to limit the 'blast radius' of a single peer's consumption.
- Connection Scopes: Leaf nodes representing the duration of a single connection.
- Stream Scopes: Leaf nodes representing the duration of a single stream.
Specialized Scopes
- Allowlist System/Transient Scopes: Used when the standard System or Transient scopes have reached their limits, but the resource originates from an allowlisted peer.
- User Transaction Scopes: Custom scopes created by a programmer as a child of any existing scope. These are used to account for delimited control flows, such as a specific Request/Response interaction within a stream.