Documentation
¶
Index ¶
- Variables
- func IsConnScope(name string) bool
- func IsSpan(name string) bool
- func IsStreamScope(name string) bool
- func IsSystemScope(name string) bool
- func IsTransientScope(name string) bool
- func NewResourceManager(limits Limiter, opts ...Option) (network.ResourceManager, error)
- func ParsePeerScopeName(name string) peer.ID
- func ParseProtocolScopeName(name string) string
- func ParseServiceScopeName(name string) string
- type Allowlist
- type BaseLimit
- func (l *BaseLimit) Apply(l2 BaseLimit)
- func (l *BaseLimit) GetConnLimit(dir network.Direction) int
- func (l *BaseLimit) GetConnTotalLimit() int
- func (l *BaseLimit) GetFDLimit() int
- func (l *BaseLimit) GetMemoryLimit() int64
- func (l *BaseLimit) GetStreamLimit(dir network.Direction) int
- func (l *BaseLimit) GetStreamTotalLimit() int
- type BaseLimitIncrease
- type Limit
- type LimitConfig
- type Limiter
- type MetricsReporter
- type Option
- type ResourceManagerStat
- type ResourceManagerState
- type ResourceScopeLimiter
- type ScalingLimitConfig
- func (cfg *ScalingLimitConfig) AddPeerLimit(p peer.ID, base BaseLimit, inc BaseLimitIncrease)
- func (cfg *ScalingLimitConfig) AddProtocolLimit(proto protocol.ID, base BaseLimit, inc BaseLimitIncrease)
- func (cfg *ScalingLimitConfig) AddProtocolPeerLimit(proto protocol.ID, base BaseLimit, inc BaseLimitIncrease)
- func (cfg *ScalingLimitConfig) AddServiceLimit(svc string, base BaseLimit, inc BaseLimitIncrease)
- func (cfg *ScalingLimitConfig) AddServicePeerLimit(svc string, base BaseLimit, inc BaseLimitIncrease)
- func (cfg *ScalingLimitConfig) AutoScale() LimitConfig
- func (cfg *ScalingLimitConfig) Scale(memory int64, numFD int) LimitConfig
- type TraceEvt
- type TraceEvtTyp
- type TraceReporter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultLimits = ScalingLimitConfig{ SystemBaseLimit: BaseLimit{ ConnsInbound: 64, ConnsOutbound: 128, Conns: 128, StreamsInbound: 64 * 16, StreamsOutbound: 128 * 16, Streams: 128 * 16, Memory: 128 << 20, FD: 256, }, SystemLimitIncrease: BaseLimitIncrease{ ConnsInbound: 64, ConnsOutbound: 128, Conns: 128, StreamsInbound: 64 * 16, StreamsOutbound: 128 * 16, Streams: 128 * 16, Memory: 1 << 30, FDFraction: 1, }, TransientBaseLimit: BaseLimit{ ConnsInbound: 32, ConnsOutbound: 64, Conns: 64, StreamsInbound: 128, StreamsOutbound: 256, Streams: 256, Memory: 32 << 20, FD: 64, }, TransientLimitIncrease: BaseLimitIncrease{ ConnsInbound: 16, ConnsOutbound: 32, Conns: 32, StreamsInbound: 128, StreamsOutbound: 256, Streams: 256, Memory: 128 << 20, FDFraction: 0.25, }, AllowlistedSystemBaseLimit: BaseLimit{ ConnsInbound: 64, ConnsOutbound: 128, Conns: 128, StreamsInbound: 64 * 16, StreamsOutbound: 128 * 16, Streams: 128 * 16, Memory: 128 << 20, FD: 256, }, AllowlistedSystemLimitIncrease: BaseLimitIncrease{ ConnsInbound: 64, ConnsOutbound: 128, Conns: 128, StreamsInbound: 64 * 16, StreamsOutbound: 128 * 16, Streams: 128 * 16, Memory: 1 << 30, FDFraction: 1, }, AllowlistedTransientBaseLimit: BaseLimit{ ConnsInbound: 32, ConnsOutbound: 64, Conns: 64, StreamsInbound: 128, StreamsOutbound: 256, Streams: 256, Memory: 32 << 20, FD: 64, }, AllowlistedTransientLimitIncrease: BaseLimitIncrease{ ConnsInbound: 16, ConnsOutbound: 32, Conns: 32, StreamsInbound: 128, StreamsOutbound: 256, Streams: 256, Memory: 128 << 20, FDFraction: 0.25, }, ServiceBaseLimit: BaseLimit{ StreamsInbound: 1024, StreamsOutbound: 4096, Streams: 4096, Memory: 64 << 20, }, ServiceLimitIncrease: BaseLimitIncrease{ StreamsInbound: 512, StreamsOutbound: 2048, Streams: 2048, Memory: 128 << 20, }, ServicePeerBaseLimit: BaseLimit{ StreamsInbound: 128, StreamsOutbound: 256, Streams: 256, Memory: 16 << 20, }, ServicePeerLimitIncrease: BaseLimitIncrease{ StreamsInbound: 4, StreamsOutbound: 8, Streams: 8, Memory: 4 << 20, }, ProtocolBaseLimit: BaseLimit{ StreamsInbound: 512, StreamsOutbound: 2048, Streams: 2048, Memory: 64 << 20, }, ProtocolLimitIncrease: BaseLimitIncrease{ StreamsInbound: 256, StreamsOutbound: 512, Streams: 512, Memory: 164 << 20, }, ProtocolPeerBaseLimit: BaseLimit{ StreamsInbound: 64, StreamsOutbound: 128, Streams: 256, Memory: 16 << 20, }, ProtocolPeerLimitIncrease: BaseLimitIncrease{ StreamsInbound: 4, StreamsOutbound: 8, Streams: 16, Memory: 4, }, PeerBaseLimit: BaseLimit{ ConnsInbound: 4, ConnsOutbound: 8, Conns: 8, StreamsInbound: 256, StreamsOutbound: 512, Streams: 512, Memory: 64 << 20, FD: 4, }, PeerLimitIncrease: BaseLimitIncrease{ StreamsInbound: 128, StreamsOutbound: 256, Streams: 256, Memory: 128 << 20, FDFraction: 1.0 / 64, }, ConnBaseLimit: BaseLimit{ ConnsInbound: 1, ConnsOutbound: 1, Conns: 1, FD: 1, Memory: 1 << 20, }, StreamBaseLimit: BaseLimit{ StreamsInbound: 1, StreamsOutbound: 1, Streams: 1, Memory: 16 << 20, }, }
DefaultLimits are the limits used by the default limiter constructors.
var InfiniteLimits = LimitConfig{
System: infiniteBaseLimit,
Transient: infiniteBaseLimit,
AllowlistedSystem: infiniteBaseLimit,
AllowlistedTransient: infiniteBaseLimit,
ServiceDefault: infiniteBaseLimit,
ServicePeerDefault: infiniteBaseLimit,
ProtocolDefault: infiniteBaseLimit,
ProtocolPeerDefault: infiniteBaseLimit,
PeerDefault: infiniteBaseLimit,
Conn: infiniteBaseLimit,
Stream: infiniteBaseLimit,
}
InfiniteLimits are a limiter configuration that uses infinite limits, thus effectively not limiting anything. Keep in mind that the operating system limits the number of file descriptors that an application can use.
Functions ¶
func IsConnScope ¶ added in v0.4.0
func IsSpan ¶ added in v0.4.0
IsSpan will return true if this name was created by newResourceScopeSpan
func IsStreamScope ¶ added in v0.4.0
func IsSystemScope ¶ added in v0.4.0
func IsTransientScope ¶ added in v0.4.0
func NewResourceManager ¶
func NewResourceManager(limits Limiter, opts ...Option) (network.ResourceManager, error)
func ParsePeerScopeName ¶ added in v0.4.0
ParsePeerScopeName returns "" if name is not a peerScopeName
func ParseProtocolScopeName ¶ added in v0.4.0
ParseProtocolScopeName returns the service name if name is a serviceScopeName. Otherwise returns ""
func ParseServiceScopeName ¶ added in v0.4.0
ParseServiceScopeName returns the service name if name is a serviceScopeName. Otherwise returns ""
Types ¶
type Allowlist ¶ added in v0.4.0
type Allowlist struct {
// contains filtered or unexported fields
}
func GetAllowlist ¶ added in v0.5.0
func GetAllowlist(rcmgr network.ResourceManager) *Allowlist
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 (*Allowlist) Add ¶ added in v0.4.0
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.
func (*Allowlist) AllowedPeerAndMultiaddr ¶ added in v0.4.0
type BaseLimit ¶
type BaseLimit struct {
Streams int
StreamsInbound int
StreamsOutbound int
Conns int
ConnsInbound int
ConnsOutbound int
FD int
Memory int64
}
BaseLimit is a mixin type for basic resource limits.
func (*BaseLimit) Apply ¶ added in v0.5.0
Apply overwrites all zero-valued limits with the values of l2 Must not use a pointer receiver.
func (*BaseLimit) GetConnTotalLimit ¶
func (*BaseLimit) GetFDLimit ¶
func (*BaseLimit) GetMemoryLimit ¶ added in v0.5.0
func (*BaseLimit) GetStreamTotalLimit ¶
type BaseLimitIncrease ¶ added in v0.5.0
type BaseLimitIncrease struct {
Streams int
StreamsInbound int
StreamsOutbound int
Conns int
ConnsInbound int
ConnsOutbound int
Memory int64
FDFraction float64
}
BaseLimitIncrease is the increase per GB of system memory.
func (*BaseLimitIncrease) Apply ¶ added in v0.5.0
func (l *BaseLimitIncrease) Apply(l2 BaseLimitIncrease)
Apply overwrites all zero-valued limits with the values of l2 Must not use a pointer receiver.
type Limit ¶
type Limit interface {
// GetMemoryLimit returns the (current) memory limit.
GetMemoryLimit() int64
// GetStreamLimit returns the stream limit, for inbound or outbound streams.
GetStreamLimit(network.Direction) int
// GetStreamTotalLimit returns the total stream limit
GetStreamTotalLimit() int
// GetConnLimit returns the connection limit, for inbound or outbound connections.
GetConnLimit(network.Direction) int
// GetConnTotalLimit returns the total connection limit
GetConnTotalLimit() int
// GetFDLimit returns the file descriptor limit.
GetFDLimit() int
}
Limit is an object that specifies basic resource limits.
type LimitConfig ¶ added in v0.5.0
type LimitConfig struct {
System BaseLimit `json:",omitempty"`
Transient BaseLimit `json:",omitempty"`
// Limits that are applied to resources with an allowlisted multiaddr.
// These will only be used if the normal System & Transient limits are
// reached.
AllowlistedSystem BaseLimit `json:",omitempty"`
AllowlistedTransient BaseLimit `json:",omitempty"`
ServiceDefault BaseLimit `json:",omitempty"`
Service map[string]BaseLimit `json:",omitempty"`
ServicePeerDefault BaseLimit `json:",omitempty"`
ServicePeer map[string]BaseLimit `json:",omitempty"`
ProtocolDefault BaseLimit `json:",omitempty"`
Protocol map[protocol.ID]BaseLimit `json:",omitempty"`
ProtocolPeerDefault BaseLimit `json:",omitempty"`
ProtocolPeer map[protocol.ID]BaseLimit `json:",omitempty"`
PeerDefault BaseLimit `json:",omitempty"`
Peer map[peer.ID]BaseLimit `json:",omitempty"`
Conn BaseLimit `json:",omitempty"`
Stream BaseLimit `json:",omitempty"`
}
func (*LimitConfig) Apply ¶ added in v0.5.0
func (cfg *LimitConfig) Apply(c LimitConfig)
type Limiter ¶
type Limiter interface {
GetSystemLimits() Limit
GetTransientLimits() Limit
GetAllowlistedSystemLimits() Limit
GetAllowlistedTransientLimits() Limit
GetServiceLimits(svc string) Limit
GetServicePeerLimits(svc string) Limit
GetProtocolLimits(proto protocol.ID) Limit
GetProtocolPeerLimits(proto protocol.ID) Limit
GetPeerLimits(p peer.ID) Limit
GetStreamLimits(p peer.ID) Limit
GetConnLimits() Limit
}
Limiter is the interface for providing limits to the resource manager.
func NewDefaultLimiterFromJSON ¶
NewDefaultLimiterFromJSON creates a new limiter by parsing a json configuration, using the default limits for fallback.
func NewFixedLimiter ¶ added in v0.5.0
func NewFixedLimiter(conf LimitConfig) Limiter
func NewLimiterFromJSON ¶
func NewLimiterFromJSON(in io.Reader, defaults LimitConfig) (Limiter, error)
NewLimiterFromJSON creates a new limiter by parsing a json configuration.
type MetricsReporter ¶ added in v0.1.4
type MetricsReporter interface {
// AllowConn is invoked when opening a connection is allowed
AllowConn(dir network.Direction, usefd bool)
// BlockConn is invoked when opening a connection is blocked
BlockConn(dir network.Direction, usefd bool)
// AllowStream is invoked when opening a stream is allowed
AllowStream(p peer.ID, dir network.Direction)
// BlockStream is invoked when opening a stream is blocked
BlockStream(p peer.ID, dir network.Direction)
// AllowPeer is invoked when attaching ac onnection to a peer is allowed
AllowPeer(p peer.ID)
// BlockPeer is invoked when attaching ac onnection to a peer is blocked
BlockPeer(p peer.ID)
// AllowProtocol is invoked when setting the protocol for a stream is allowed
AllowProtocol(proto protocol.ID)
// BlockProtocol is invoked when setting the protocol for a stream is blocked
BlockProtocol(proto protocol.ID)
// BlockProtocolPeer is invoked when setting the protocol for a stream is blocked at the per protocol peer scope
BlockProtocolPeer(proto protocol.ID, p peer.ID)
// AllowService is invoked when setting the protocol for a stream is allowed
AllowService(svc string)
// BlockService is invoked when setting the protocol for a stream is blocked
BlockService(svc string)
// BlockServicePeer is invoked when setting the service for a stream is blocked at the per service peer scope
BlockServicePeer(svc string, p peer.ID)
// AllowMemory is invoked when a memory reservation is allowed
AllowMemory(size int)
// BlockMemory is invoked when a memory reservation is blocked
BlockMemory(size int)
}
MetricsReporter is an interface for collecting metrics from resource manager actions
type Option ¶
type Option func(*resourceManager) error
func WithAllowlistedMultiaddrs ¶ added in v0.4.0
WithAllowlistedMultiaddrs sets the multiaddrs to be in the allowlist
Example ¶
somePeer, err := test.RandPeerID()
if err != nil {
panic("Failed to generate somePeer")
}
limits := DefaultLimits.AutoScale()
rcmgr, err := NewResourceManager(NewFixedLimiter(limits), WithAllowlistedMultiaddrs([]multiaddr.Multiaddr{
// Any peer connecting from this IP address
multiaddr.StringCast("/ip4/1.2.3.4"),
// Only the specified peer from this address
multiaddr.StringCast("/ip4/2.2.3.4/p2p/" + peer.Encode(somePeer)),
// Only peers from this 1.2.3.0/24 IP address range
multiaddr.StringCast("/ip4/1.2.3.0/ipcidr/24"),
}))
if err != nil {
panic("Failed to start resource manager")
}
// Use rcmgr as before
_ = rcmgr
func WithMetrics ¶ added in v0.1.4
func WithMetrics(reporter MetricsReporter) Option
WithMetrics is a resource manager option to enable metrics collection
func WithTraceReporter ¶ added in v0.4.0
func WithTraceReporter(reporter TraceReporter) Option
type ResourceManagerStat ¶
type ResourceManagerState ¶
type ResourceManagerState interface {
ListServices() []string
ListProtocols() []protocol.ID
ListPeers() []peer.ID
Stat() ResourceManagerStat
}
ResourceManagerStat is a trait that allows you to access resource manager state.
type ResourceScopeLimiter ¶
ResourceScopeLimiter is a trait interface that allows you to access scope limits.
type ScalingLimitConfig ¶ added in v0.5.0
type ScalingLimitConfig struct {
SystemBaseLimit BaseLimit
SystemLimitIncrease BaseLimitIncrease
TransientBaseLimit BaseLimit
TransientLimitIncrease BaseLimitIncrease
AllowlistedSystemBaseLimit BaseLimit
AllowlistedSystemLimitIncrease BaseLimitIncrease
AllowlistedTransientBaseLimit BaseLimit
AllowlistedTransientLimitIncrease BaseLimitIncrease
ServiceBaseLimit BaseLimit
ServiceLimitIncrease BaseLimitIncrease
ServiceLimits map[string]baseLimitConfig // use AddServiceLimit to modify
ServicePeerBaseLimit BaseLimit
ServicePeerLimitIncrease BaseLimitIncrease
ServicePeerLimits map[string]baseLimitConfig // use AddServicePeerLimit to modify
ProtocolBaseLimit BaseLimit
ProtocolLimitIncrease BaseLimitIncrease
ProtocolLimits map[protocol.ID]baseLimitConfig // use AddProtocolLimit to modify
ProtocolPeerBaseLimit BaseLimit
ProtocolPeerLimitIncrease BaseLimitIncrease
ProtocolPeerLimits map[protocol.ID]baseLimitConfig // use AddProtocolPeerLimit to modify
PeerBaseLimit BaseLimit
PeerLimitIncrease BaseLimitIncrease
PeerLimits map[peer.ID]baseLimitConfig // use AddPeerLimit to modify
ConnBaseLimit BaseLimit
ConnLimitIncrease BaseLimitIncrease
StreamBaseLimit BaseLimit
StreamLimitIncrease BaseLimitIncrease
}
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.
func (*ScalingLimitConfig) AddPeerLimit ¶ added in v0.5.0
func (cfg *ScalingLimitConfig) AddPeerLimit(p peer.ID, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig) AddProtocolLimit ¶ added in v0.5.0
func (cfg *ScalingLimitConfig) AddProtocolLimit(proto protocol.ID, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig) AddProtocolPeerLimit ¶ added in v0.5.0
func (cfg *ScalingLimitConfig) AddProtocolPeerLimit(proto protocol.ID, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig) AddServiceLimit ¶ added in v0.5.0
func (cfg *ScalingLimitConfig) AddServiceLimit(svc string, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig) AddServicePeerLimit ¶ added in v0.5.0
func (cfg *ScalingLimitConfig) AddServicePeerLimit(svc string, base BaseLimit, inc BaseLimitIncrease)
func (*ScalingLimitConfig) AutoScale ¶ added in v0.5.0
func (cfg *ScalingLimitConfig) AutoScale() LimitConfig
func (*ScalingLimitConfig) Scale ¶ added in v0.5.0
func (cfg *ScalingLimitConfig) Scale(memory int64, numFD int) LimitConfig
Scale scales up a limit configuration. memory is the amount of memory that the stack is allowed to consume, for a full 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.
type TraceEvt ¶ added in v0.4.0
type TraceEvt struct {
Time string
Type TraceEvtTyp
Scope *scopeClass `json:",omitempty"`
Name string `json:",omitempty"`
Limit interface{} `json:",omitempty"`
Priority uint8 `json:",omitempty"`
Delta int64 `json:",omitempty"`
DeltaIn int `json:",omitempty"`
DeltaOut int `json:",omitempty"`
Memory int64 `json:",omitempty"`
StreamsIn int `json:",omitempty"`
StreamsOut int `json:",omitempty"`
ConnsIn int `json:",omitempty"`
ConnsOut int `json:",omitempty"`
FD int `json:",omitempty"`
}
type TraceEvtTyp ¶ added in v0.4.0
type TraceEvtTyp string
const ( TraceStartEvt TraceEvtTyp = "start" TraceCreateScopeEvt TraceEvtTyp = "create_scope" TraceDestroyScopeEvt TraceEvtTyp = "destroy_scope" TraceReserveMemoryEvt TraceEvtTyp = "reserve_memory" TraceBlockReserveMemoryEvt TraceEvtTyp = "block_reserve_memory" TraceReleaseMemoryEvt TraceEvtTyp = "release_memory" TraceAddStreamEvt TraceEvtTyp = "add_stream" TraceBlockAddStreamEvt TraceEvtTyp = "block_add_stream" TraceRemoveStreamEvt TraceEvtTyp = "remove_stream" TraceAddConnEvt TraceEvtTyp = "add_conn" TraceBlockAddConnEvt TraceEvtTyp = "block_add_conn" TraceRemoveConnEvt TraceEvtTyp = "remove_conn" )
type TraceReporter ¶ added in v0.4.0
type TraceReporter interface {
// ConsumeEvent consumes a trace event. This is called synchronously,
// implementations should process the event quickly.
ConsumeEvent(TraceEvt)
}