2022-01-17 15:15:05 +08:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2022-08-18 23:54:48 +08:00
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
2022-01-17 15:15:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResourceManager is the interface to the network resource management subsystem.
|
|
|
|
// The ResourceManager tracks and accounts for resource usage in the stack, from the internals
|
|
|
|
// to the application, and provides a mechanism to limit resource usage according to a user
|
|
|
|
// configurable policy.
|
|
|
|
//
|
|
|
|
// Resource Management through the ResourceManager is based on the concept of Resource
|
|
|
|
// Management Scopes, whereby resource usage is constrained by a DAG of scopes,
|
|
|
|
// The following diagram illustrates the structure of the resource constraint DAG:
|
|
|
|
// System
|
2022-08-18 23:54:48 +08:00
|
|
|
//
|
|
|
|
// +------------> Transient.............+................+
|
|
|
|
// | . .
|
|
|
|
// +------------> Service------------- . ----------+ .
|
|
|
|
// | . | .
|
|
|
|
// +-------------> Protocol----------- . ----------+ .
|
|
|
|
// | . | .
|
|
|
|
// +--------------> Peer \ | .
|
|
|
|
// +------------> Connection | .
|
|
|
|
// | \ \
|
|
|
|
// +---------------------------> Stream
|
2022-01-17 15:15:05 +08:00
|
|
|
//
|
|
|
|
// The basic resources accounted by the ResourceManager include memory, streams, connections,
|
|
|
|
// and file descriptors. These account for both space and time used by
|
|
|
|
// the stack, as each resource has a direct effect on the system
|
|
|
|
// availability and performance.
|
|
|
|
//
|
|
|
|
// The modus operandi of the resource manager is to restrict resource usage at the time of
|
|
|
|
// reservation. When a component of the stack needs to use a resource, it reserves it in the
|
|
|
|
// appropriate scope. The resource manager gates the reservation against the scope applicable
|
|
|
|
// limits; if the limit is exceeded, then an error (wrapping ErrResourceLimitExceeded) and it
|
|
|
|
// is up the component to act accordingly. At the lower levels of the stack, this will normally
|
|
|
|
// signal a failure of some sorts, like failing to opening a stream or a connection, which will
|
|
|
|
// propagate to the programmer. Some components may be able to handle resource reservation failure
|
|
|
|
// more gracefully; for instance a muxer trying to grow a buffer for a window change, will simply
|
|
|
|
// retain the existing window size and continue to operate normally albeit with some degraded
|
|
|
|
// throughput.
|
|
|
|
// All resources reserved in some scope are released when the scope is closed. For low level
|
|
|
|
// scopes, mainly Connection and Stream scopes, this happens when the connection or stream is
|
|
|
|
// closed.
|
|
|
|
//
|
|
|
|
// Service programmers will typically use the resource manager to reserve memory
|
|
|
|
// for their subsystem.
|
|
|
|
// This happens with two avenues: the programmer can attach a stream to a service, whereby
|
|
|
|
// resources reserved by the stream are automatically accounted in the service budget; or the
|
|
|
|
// programmer may directly interact with the service scope, by using ViewService through the
|
|
|
|
// resource manager interface.
|
|
|
|
//
|
|
|
|
// Application programmers can also directly reserve memory in some applicable scope. In order
|
|
|
|
// to facilitate control flow delimited resource accounting, all scopes defined in the system
|
|
|
|
// allow for the user to create spans. Spans are temporary scopes rooted at some
|
|
|
|
// other scope and release their resources when the programmer is done with them. Span
|
|
|
|
// scopes can form trees, with nested spans.
|
|
|
|
//
|
|
|
|
// Typical Usage:
|
2022-08-18 23:54:48 +08:00
|
|
|
// - Low level components of the system (transports, muxers) all have access to the resource
|
|
|
|
// manager and create connection and stream scopes through it. These scopes are accessible
|
|
|
|
// to the user, albeit with a narrower interface, through Conn and Stream objects who have
|
|
|
|
// a Scope method.
|
|
|
|
// - Services typically center around streams, where the programmer can attach streams to a
|
|
|
|
// particular service. They can also directly reserve memory for a service by accessing the
|
|
|
|
// service scope using the ResourceManager interface.
|
|
|
|
// - Applications that want to account for their network resource usage can reserve memory,
|
|
|
|
// typically using a span, directly in the System or a Service scope; they can also
|
|
|
|
// opt to use appropriate steam scopes for streams that they create or own.
|
2022-01-17 15:15:05 +08:00
|
|
|
//
|
|
|
|
// User Serviceable Parts: the user has the option to specify their own implementation of the
|
|
|
|
// interface. We provide a canonical implementation in the go-libp2p-resource-manager package.
|
|
|
|
// The user of that package can specify limits for the various scopes, which can be static
|
|
|
|
// or dynamic.
|
|
|
|
//
|
|
|
|
// WARNING The ResourceManager interface is considered experimental and subject to change
|
2022-08-18 23:54:48 +08:00
|
|
|
//
|
|
|
|
// in subsequent releases.
|
|
|
|
//
|
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ResourceManager instead
|
|
|
|
type ResourceManager = network.ResourceManager
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
// ResourceScopeViewer is a mixin interface providing view methods for accessing top level
|
|
|
|
// scopes.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ResourceScopeViewer instead
|
|
|
|
type ResourceScopeViewer = network.ResourceScopeViewer
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
const (
|
|
|
|
// ReservationPriorityLow is a reservation priority that indicates a reservation if the scope
|
|
|
|
// memory utilization is at 40% or less.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ReservationPriorityLow instead
|
|
|
|
ReservationPriorityLow = network.ReservationPriorityLow
|
|
|
|
// ReservationPriorityMedium is a reservation priority that indicates a reservation if the scope
|
2022-01-17 15:15:05 +08:00
|
|
|
// memory utilization is at 60% or less.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ReservationPriorityMedium instead
|
|
|
|
ReservationPriorityMedium uint8 = network.ReservationPriorityMedium
|
2022-01-17 15:15:05 +08:00
|
|
|
// ReservationPriorityHigh is a reservation prioirity that indicates a reservation if the scope
|
|
|
|
// memory utilization is at 80% or less.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ReservationPriorityHigh instead
|
|
|
|
ReservationPriorityHigh uint8 = network.ReservationPriorityHigh
|
2022-01-17 15:15:05 +08:00
|
|
|
// ReservationPriorityAlways is a reservation priority that indicates a reservation if there is
|
|
|
|
// enough memory, regardless of scope utilization.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ReservationPriorityAlways instead
|
|
|
|
ReservationPriorityAlways = network.ReservationPriorityAlways
|
2022-01-17 15:15:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResourceScope is the interface for all scopes.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ResourceScope instead
|
|
|
|
type ResourceScope = network.ResourceScope
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
// ResourceScopeSpan is a ResourceScope with a delimited span.
|
|
|
|
// Span scopes are control flow delimited and release all their associated resources
|
|
|
|
// when the programmer calls Done.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2022-08-18 23:54:48 +08:00
|
|
|
// s, err := someScope.BeginSpan()
|
|
|
|
// if err != nil { ... }
|
|
|
|
// defer s.Done()
|
|
|
|
//
|
|
|
|
// if err := s.ReserveMemory(...); err != nil { ... }
|
|
|
|
// // ... use memory
|
|
|
|
//
|
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ResourceScopeSpan instead
|
|
|
|
type ResourceScopeSpan = network.ResourceScopeSpan
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
// ServiceScope is the interface for service resource scopes
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ServiceScope instead
|
|
|
|
type ServiceScope = network.ServiceScope
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
// ProtocolScope is the interface for protocol resource scopes.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ProtocolScope instead
|
|
|
|
type ProtocolScope = network.ProtocolScope
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
// PeerScope is the interface for peer resource scopes.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.PeerScope instead
|
|
|
|
type PeerScope = network.PeerScope
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
// ConnManagementScope is the low level interface for connection resource scopes.
|
|
|
|
// This interface is used by the low level components of the system who create and own
|
|
|
|
// the span of a connection scope.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ConnManagementScope instead
|
|
|
|
type ConnManagementScope = network.ConnManagementScope
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
// ConnScope is the user view of a connection scope
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ConnScope instead
|
|
|
|
type ConnScope = network.ConnScope
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
// StreamManagementScope is the interface for stream resource scopes.
|
|
|
|
// This interface is used by the low level components of the system who create and own
|
|
|
|
// the span of a stream scope.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.StreamManagementScope instead
|
|
|
|
type StreamManagementScope = network.StreamManagementScope
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
// StreamScope is the user view of a StreamScope.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.StreamScope instead
|
|
|
|
type StreamScope = network.StreamScope
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
// ScopeStat is a struct containing resource accounting information.
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.ScopeStat instead
|
|
|
|
type ScopeStat = network.ScopeStat
|
2022-01-17 15:15:05 +08:00
|
|
|
|
|
|
|
// NullResourceManager is a stub for tests and initialization of default values
|
2022-08-18 23:54:48 +08:00
|
|
|
// Deprecated: use github.com/libp2p/go-libp2p/core/network.NullResourceManager instead
|
|
|
|
var NullResourceManager = network.NullResourceManager
|