package rcmgr
Import Path
github.com/libp2p/go-libp2p/p2p/host/resource-manager (on go.dev)
Dependency Relation
imports 29 packages, and imported by 2 packages
Involved Source Files
allowlist.go
conn_limiter.go
conn_rate_limiter.go
error.go
extapi.go
Package rcmgr is the resource manager for go-libp2p. This allows you to track
resources being used throughout your go-libp2p process. As well as making sure
that the process doesn't use more resources than what you define as your
limits. The resource manager only knows about things it is told about, so it's
the responsibility of the user of this library (either go-libp2p or a go-libp2p
user) to make sure they check with the resource manager before actually
allocating the resource.
limit_defaults.go
metrics.go
rcmgr.go
scope.go
stats.go
sys_unix.go
trace.go
Package-Level Type Names (total 24)
Add takes a multiaddr and adds it to the allowlist. The multiaddr should be
an ip address of the peer with or without a `/p2p` protocol.
e.g. /ip4/1.2.3.4/p2p/QmFoo, /ip4/1.2.3.4, and /ip4/1.2.3.0/ipcidr/24 are valid.
/p2p/QmFoo is not valid.
(*Allowlist) Allowed(ma multiaddr.Multiaddr) bool
(*Allowlist) AllowedPeerAndMultiaddr(peerID peer.ID, ma multiaddr.Multiaddr) bool
(*Allowlist) Remove(ma multiaddr.Multiaddr) error
func GetAllowlist(rcmgr network.ResourceManager) *Allowlist
BaseLimit is a mixin type for basic resource limits.
Conns int
ConnsInbound int
ConnsOutbound int
FD int
Memory int64
Streams int
StreamsInbound int
StreamsOutbound int
Apply overwrites all zero-valued limits with the values of l2
Must not use a pointer receiver.
( BaseLimit) GetConnLimit(dir network.Direction) int
( BaseLimit) GetConnTotalLimit() int
( BaseLimit) GetFDLimit() int
( BaseLimit) GetMemoryLimit() int64
( BaseLimit) GetStreamLimit(dir network.Direction) int
( BaseLimit) GetStreamTotalLimit() int
ToResourceLimits converts the BaseLimit to a ResourceLimits
BaseLimit : Limit
func (*ResourceLimits).Build(defaults Limit) BaseLimit
func (*BaseLimit).Apply(l2 BaseLimit)
func (*ScalingLimitConfig).AddPeerLimit(p peer.ID, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig).AddProtocolLimit(proto protocol.ID, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig).AddProtocolPeerLimit(proto protocol.ID, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig).AddServiceLimit(svc string, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig).AddServicePeerLimit(svc string, base BaseLimit, inc BaseLimitIncrease)
BaseLimitIncrease is the increase per GiB of allowed memory.
Conns int
ConnsInbound int
ConnsOutbound int
FDFraction is expected to be >= 0 and <= 1.
Memory is in bytes. Values over 1>>30 (1GiB) don't make sense.
Streams int
StreamsInbound int
StreamsOutbound int
Apply overwrites all zero-valued limits with the values of l2
Must not use a pointer receiver.
func (*BaseLimitIncrease).Apply(l2 BaseLimitIncrease)
func (*ScalingLimitConfig).AddPeerLimit(p peer.ID, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig).AddProtocolLimit(proto protocol.ID, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig).AddProtocolPeerLimit(proto protocol.ID, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig).AddServiceLimit(svc string, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig).AddServicePeerLimit(svc string, base BaseLimit, inc BaseLimitIncrease)
ConcreteLimitConfig is similar to PartialLimitConfig, but all values are defined.
There is no unset "default" value. Commonly constructed by calling
PartialLimitConfig.Build(rcmgr.DefaultLimits.AutoScale())
ToPartialLimitConfig converts a ConcreteLimitConfig to a PartialLimitConfig.
The returned PartialLimitConfig will have no default values.
func PartialLimitConfig.Build(defaults ConcreteLimitConfig) ConcreteLimitConfig
func (*ScalingLimitConfig).AutoScale() ConcreteLimitConfig
func (*ScalingLimitConfig).Scale(memory int64, numFD int) ConcreteLimitConfig
func NewFixedLimiter(conf ConcreteLimitConfig) Limiter
func NewLimiterFromJSON(in io.Reader, defaults ConcreteLimitConfig) (Limiter, error)
func PartialLimitConfig.Build(defaults ConcreteLimitConfig) ConcreteLimitConfig
var InfiniteLimits
The maximum number of connections allowed for each subnet.
This defines how big the subnet is. For example, a /24 subnet has a
PrefixLength of 24. All IPs that share the same 24 bit prefix are in the same
subnet. Are in the same subnet, and bound to the same limit.
func WithLimitPerSubnet(ipv4 []ConnLimitPerSubnet, ipv6 []ConnLimitPerSubnet) Option
func WithLimitPerSubnet(ipv4 []ConnLimitPerSubnet, ipv6 []ConnLimitPerSubnet) Option
(*ErrMemoryLimitExceeded) Error() string
(*ErrMemoryLimitExceeded) Unwrap() error
*ErrMemoryLimitExceeded : error
*ErrMemoryLimitExceeded : golang.org/x/xerrors.Wrapper
(*ErrStreamOrConnLimitExceeded) Error() string
(*ErrStreamOrConnLimitExceeded) Unwrap() error
*ErrStreamOrConnLimitExceeded : error
*ErrStreamOrConnLimitExceeded : golang.org/x/xerrors.Wrapper
Limit is an object that specifies basic resource limits.
GetConnLimit returns the connection limit, for inbound or outbound connections.
GetConnTotalLimit returns the total connection limit
GetFDLimit returns the file descriptor limit.
GetMemoryLimit returns the (current) memory limit.
GetStreamLimit returns the stream limit, for inbound or outbound streams.
GetStreamTotalLimit returns the total stream limit
BaseLimit
func Limiter.GetAllowlistedSystemLimits() Limit
func Limiter.GetAllowlistedTransientLimits() Limit
func Limiter.GetConnLimits() Limit
func Limiter.GetPeerLimits(p peer.ID) Limit
func Limiter.GetProtocolLimits(proto protocol.ID) Limit
func Limiter.GetProtocolPeerLimits(proto protocol.ID) Limit
func Limiter.GetServiceLimits(svc string) Limit
func Limiter.GetServicePeerLimits(svc string) Limit
func Limiter.GetStreamLimits(p peer.ID) Limit
func Limiter.GetSystemLimits() Limit
func Limiter.GetTransientLimits() Limit
func ResourceScopeLimiter.Limit() Limit
func (*ResourceLimits).Build(defaults Limit) BaseLimit
func ResourceScopeLimiter.SetLimit(Limit)
Limiter is the interface for providing limits to the resource manager.
( Limiter) GetAllowlistedSystemLimits() Limit
( Limiter) GetAllowlistedTransientLimits() Limit
( Limiter) GetConnLimits() Limit
( Limiter) GetPeerLimits(p peer.ID) Limit
( Limiter) GetProtocolLimits(proto protocol.ID) Limit
( Limiter) GetProtocolPeerLimits(proto protocol.ID) Limit
( Limiter) GetServiceLimits(svc string) Limit
( Limiter) GetServicePeerLimits(svc string) Limit
( Limiter) GetStreamLimits(p peer.ID) Limit
( Limiter) GetSystemLimits() Limit
( Limiter) GetTransientLimits() Limit
func NewDefaultLimiterFromJSON(in io.Reader) (Limiter, error)
func NewFixedLimiter(conf ConcreteLimitConfig) Limiter
func NewLimiterFromJSON(in io.Reader, defaults ConcreteLimitConfig) (Limiter, error)
func NewResourceManager(limits Limiter, opts ...Option) (network.ResourceManager, error)
( LimitVal) Build(defaultVal int) int
( LimitVal) MarshalJSON() ([]byte, error)
(*LimitVal) UnmarshalJSON(b []byte) error
LimitVal : github.com/goccy/go-json.Marshaler
*LimitVal : github.com/goccy/go-json.Unmarshaler
LimitVal : encoding/json.Marshaler
*LimitVal : encoding/json.Unmarshaler
const BlockAllLimit
const DefaultLimit
const Unlimited
( LimitVal64) Build(defaultVal int64) int64
( LimitVal64) MarshalJSON() ([]byte, error)
(*LimitVal64) UnmarshalJSON(b []byte) error
LimitVal64 : github.com/goccy/go-json.Marshaler
*LimitVal64 : github.com/goccy/go-json.Unmarshaler
LimitVal64 : encoding/json.Marshaler
*LimitVal64 : encoding/json.Unmarshaler
const BlockAllLimit64
const DefaultLimit64
const Unlimited64
MetricsReporter is an interface for collecting metrics from resource manager actions
AllowConn is invoked when opening a connection is allowed
AllowMemory is invoked when a memory reservation is allowed
AllowPeer is invoked when attaching ac onnection to a peer is allowed
AllowProtocol is invoked when setting the protocol for a stream is allowed
AllowService is invoked when setting the protocol for a stream is allowed
AllowStream is invoked when opening a stream is allowed
BlockConn is invoked when opening a connection is blocked
BlockMemory is invoked when a memory reservation is blocked
BlockPeer is invoked when attaching ac onnection to a peer is blocked
BlockProtocol is invoked when setting the protocol for a stream is blocked
BlockProtocolPeer is invoked when setting the protocol for a stream is blocked at the per protocol peer scope
BlockService is invoked when setting the protocol for a stream is blocked
BlockServicePeer is invoked when setting the service for a stream is blocked at the per service peer scope
BlockStream is invoked when opening a stream is blocked
func WithMetrics(reporter MetricsReporter) Option
The maximum number of connections allowed for this subnet.
The Network prefix for which this limit applies.
func WithNetworkPrefixLimit(ipv4 []NetworkPrefixLimit, ipv6 []NetworkPrefixLimit) Option
func WithNetworkPrefixLimit(ipv4 []NetworkPrefixLimit, ipv6 []NetworkPrefixLimit) Option
func WithAllowlistedMultiaddrs(mas []multiaddr.Multiaddr) Option
func WithConnRateLimiters(connRateLimiter *rate.Limiter) Option
func WithLimitPerSubnet(ipv4 []ConnLimitPerSubnet, ipv6 []ConnLimitPerSubnet) Option
func WithMetrics(reporter MetricsReporter) Option
func WithMetricsDisabled() Option
func WithNetworkPrefixLimit(ipv4 []NetworkPrefixLimit, ipv6 []NetworkPrefixLimit) Option
func WithTrace(path string) Option
func WithTraceReporter(reporter TraceReporter) Option
func NewResourceManager(limits Limiter, opts ...Option) (network.ResourceManager, error)
Limits that are applied to resources with an allowlisted multiaddr.
These will only be used if the normal System & Transient limits are
reached.
AllowlistedTransient ResourceLimits
Conn ResourceLimits
Peer map[peer.ID]ResourceLimits
PeerDefault ResourceLimits
Protocol map[protocol.ID]ResourceLimits
ProtocolDefault ResourceLimits
ProtocolPeer map[protocol.ID]ResourceLimits
ProtocolPeerDefault ResourceLimits
Service map[string]ResourceLimits
ServiceDefault ResourceLimits
ServicePeer map[string]ResourceLimits
ServicePeerDefault ResourceLimits
Stream ResourceLimits
System ResourceLimits
Transient ResourceLimits
(*PartialLimitConfig) Apply(c PartialLimitConfig)
( PartialLimitConfig) Build(defaults ConcreteLimitConfig) ConcreteLimitConfig
(*PartialLimitConfig) MarshalJSON() ([]byte, error)
*PartialLimitConfig : github.com/goccy/go-json.Marshaler
*PartialLimitConfig : encoding/json.Marshaler
func ConcreteLimitConfig.ToPartialLimitConfig() PartialLimitConfig
func (*PartialLimitConfig).Apply(c PartialLimitConfig)
ResourceLimits is the type for basic resource limits.
Conns LimitVal
ConnsInbound LimitVal
ConnsOutbound LimitVal
FD LimitVal
Memory LimitVal64
Streams LimitVal
StreamsInbound LimitVal
StreamsOutbound LimitVal
Apply overwrites all default limits with the values of l2
(*ResourceLimits) Build(defaults Limit) BaseLimit
(*ResourceLimits) IsDefault() bool
(*ResourceLimits) ToMaybeNilPtr() *ResourceLimits
func BaseLimit.ToResourceLimits() ResourceLimits
func (*ResourceLimits).ToMaybeNilPtr() *ResourceLimits
func (*ResourceLimits).Apply(l2 ResourceLimits)
Peers map[peer.ID]network.ScopeStat
Protocols map[protocol.ID]network.ScopeStat
Services map[string]network.ScopeStat
System network.ScopeStat
Transient network.ScopeStat
func ResourceManagerState.Stat() ResourceManagerStat
ResourceManagerStat is a trait that allows you to access resource manager state.
( ResourceManagerState) ListPeers() []peer.ID
( ResourceManagerState) ListProtocols() []protocol.ID
( ResourceManagerState) ListServices() []string
( ResourceManagerState) Stat() ResourceManagerStat
ResourceScopeLimiter is a trait interface that allows you to access scope limits.
( ResourceScopeLimiter) Limit() Limit
( ResourceScopeLimiter) SetLimit(Limit)
ScalingLimitConfig is a struct for configuring default limits.
{}BaseLimit is the limits that Apply for a minimal node (128 MB of memory for libp2p) and 256 file descriptors.
{}LimitIncrease is the additional limit granted for every additional 1 GB of RAM.
AllowlistedSystemBaseLimit BaseLimit
AllowlistedSystemLimitIncrease BaseLimitIncrease
AllowlistedTransientBaseLimit BaseLimit
AllowlistedTransientLimitIncrease BaseLimitIncrease
ConnBaseLimit BaseLimit
ConnLimitIncrease BaseLimitIncrease
PeerBaseLimit BaseLimit
PeerLimitIncrease BaseLimitIncrease
// use AddPeerLimit to modify
ProtocolBaseLimit BaseLimit
ProtocolLimitIncrease BaseLimitIncrease
// use AddProtocolLimit to modify
ProtocolPeerBaseLimit BaseLimit
ProtocolPeerLimitIncrease BaseLimitIncrease
// use AddProtocolPeerLimit to modify
ServiceBaseLimit BaseLimit
ServiceLimitIncrease BaseLimitIncrease
// use AddServiceLimit to modify
ServicePeerBaseLimit BaseLimit
ServicePeerLimitIncrease BaseLimitIncrease
// use AddServicePeerLimit to modify
StreamBaseLimit BaseLimit
StreamLimitIncrease BaseLimitIncrease
SystemBaseLimit BaseLimit
SystemLimitIncrease BaseLimitIncrease
TransientBaseLimit BaseLimit
TransientLimitIncrease BaseLimitIncrease
(*ScalingLimitConfig) AddPeerLimit(p peer.ID, base BaseLimit, inc BaseLimitIncrease)
(*ScalingLimitConfig) AddProtocolLimit(proto protocol.ID, base BaseLimit, inc BaseLimitIncrease)
(*ScalingLimitConfig) AddProtocolPeerLimit(proto protocol.ID, base BaseLimit, inc BaseLimitIncrease)
(*ScalingLimitConfig) AddServiceLimit(svc string, base BaseLimit, inc BaseLimitIncrease)
(*ScalingLimitConfig) AddServicePeerLimit(svc string, base BaseLimit, inc BaseLimitIncrease)
(*ScalingLimitConfig) AutoScale() ConcreteLimitConfig
Scale scales up a limit configuration.
memory is the amount of memory that the stack is allowed to consume,
for a dedicated node it's recommended to use 1/8 of the installed system memory.
If memory is smaller than 128 MB, the base configuration will be used.
func github.com/libp2p/go-libp2p.SetDefaultServiceLimits(config *ScalingLimitConfig)
var DefaultLimits
StatsTraceReporter reports stats on the resource manager using its traces.
( StatsTraceReporter) ConsumeEvent(evt TraceEvt)
StatsTraceReporter : TraceReporter
func NewStatsTraceReporter() (StatsTraceReporter, error)
ConnsIn int
ConnsOut int
Delta int64
DeltaIn int
DeltaOut int
FD int
Limit interface{}
Memory int64
Name string
Priority uint8
Scope *scopeClass
StreamsIn int
StreamsOut int
Time string
Type TraceEvtTyp
func StatsTraceReporter.ConsumeEvent(evt TraceEvt)
func TraceReporter.ConsumeEvent(TraceEvt)
const TraceAddConnEvt
const TraceAddStreamEvt
const TraceBlockAddConnEvt
const TraceBlockAddStreamEvt
const TraceBlockReserveMemoryEvt
const TraceCreateScopeEvt
const TraceDestroyScopeEvt
const TraceReleaseMemoryEvt
const TraceRemoveConnEvt
const TraceRemoveStreamEvt
const TraceReserveMemoryEvt
const TraceStartEvt
ConsumeEvent consumes a trace event. This is called synchronously,
implementations should process the event quickly.
StatsTraceReporter
func WithTraceReporter(reporter TraceReporter) Option
Package-Level Functions (total 22)
GetAllowlist tries to get the allowlist from the given resourcemanager
interface by checking to see if its concrete type is a resourceManager.
Returns nil if it fails to get the allowlist.
func IsConnScope(name string) bool
IsSpan will return true if this name was created by newResourceScopeSpan
func IsStreamScope(name string) bool func IsSystemScope(name string) bool func IsTransientScope(name string) bool func MustRegisterWith(reg prometheus.Registerer)
NewDefaultLimiterFromJSON creates a new limiter by parsing a json configuration,
using the default limits for fallback.
func NewFixedLimiter(conf ConcreteLimitConfig) Limiter
NewLimiterFromJSON creates a new limiter by parsing a json configuration.
func NewResourceManager(limits Limiter, opts ...Option) (network.ResourceManager, error) func NewStatsTraceReporter() (StatsTraceReporter, error)
ParseProtocolScopeName returns the service name if name is a serviceScopeName.
Otherwise returns ""
PeerStrInScopeName returns "" if name is not a peerScopeName. Returns a string to avoid allocating a peer ID object
WithAllowlistedMultiaddrs sets the multiaddrs to be in the allowlist
WithConnRateLimiters sets a custom rate limiter for new connections.
connRateLimiter is used for OpenConnection calls
WithLimitPerSubnet sets the limits for the number of connections allowed per
subnet. This will limit the number of connections per subnet if that subnet
is not defined in the NetworkPrefixLimit option. Think of this as a default
limit for any given subnet.
WithMetrics is a resource manager option to enable metrics collection
func WithMetricsDisabled() Option
WithNetworkPrefixLimit sets the limits for the number of connections allowed
for a specific Network Prefix. Use this when you want to set higher limits
for a specific subnet than the default limit per subnet.
func WithTraceReporter(reporter TraceReporter) Option
Package-Level Variables (total 4)
DefaultLimits are the limits used by the default limiter constructors.
InfiniteLimits are a limiter configuration that uses unlimited limits, thus effectively not limiting anything.
Keep in mind that the operating system limits the number of file descriptors that an application can use.
Package-Level Constants (total 18)
BlockAllLimit is the LimitVal for allowing no amount of resources.
BlockAllLimit64 is the LimitVal for allowing no amount of resources.
DefaultLimit is the default value for resources. The exact value depends on the context, but will get values from `DefaultLimits`.
Default is the default value for resources.
const TraceAddConnEvt TraceEvtTyp = "add_conn" const TraceAddStreamEvt TraceEvtTyp = "add_stream" const TraceBlockAddConnEvt TraceEvtTyp = "block_add_conn" const TraceBlockAddStreamEvt TraceEvtTyp = "block_add_stream" const TraceBlockReserveMemoryEvt TraceEvtTyp = "block_reserve_memory" const TraceCreateScopeEvt TraceEvtTyp = "create_scope" const TraceDestroyScopeEvt TraceEvtTyp = "destroy_scope" const TraceReleaseMemoryEvt TraceEvtTyp = "release_memory" const TraceRemoveConnEvt TraceEvtTyp = "remove_conn" const TraceRemoveStreamEvt TraceEvtTyp = "remove_stream" const TraceReserveMemoryEvt TraceEvtTyp = "reserve_memory" const TraceStartEvt TraceEvtTyp = "start"
Unlimited is the value for unlimited resources. An arbitrarily high number will also work.
Unlimited is the value for unlimited resources.
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |