go-libp2p-resource-manager/limit.go

188 lines
4.9 KiB
Go
Raw Normal View History

2021-12-22 17:39:46 +08:00
package rcmgr
2021-12-23 21:28:14 +08:00
import (
"github.com/libp2p/go-libp2p-core/network"
2021-12-23 23:46:08 +08:00
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
2021-12-23 21:28:14 +08:00
)
2022-01-05 22:54:28 +08:00
// Limit is an object that specifies basic resource limits.
2021-12-22 17:39:46 +08:00
type Limit interface {
// GetMemoryLimit returns the (current) memory limit.
2021-12-22 17:39:46 +08:00
GetMemoryLimit() int64
// GetStreamLimit returns the stream limit, for inbound or outbound streams.
2021-12-23 21:28:14 +08:00
GetStreamLimit(network.Direction) int
2022-01-15 02:10:18 +08:00
// GetStreamTotalLimit returns the total stream limit
GetStreamTotalLimit() int
// GetConnLimit returns the connection limit, for inbound or outbound connections.
2021-12-23 21:28:14 +08:00
GetConnLimit(network.Direction) int
2022-01-15 02:10:18 +08:00
// GetConnTotalLimit returns the total connection limit
GetConnTotalLimit() int
// GetFDLimit returns the file descriptor limit.
2021-12-23 21:28:14 +08:00
GetFDLimit() int
// WithMemoryLimit creates a copy of this limit object, with memory limit adjusted to
// the specified memFraction of its current value, bounded by minMemory and maxMemory.
WithMemoryLimit(memFraction float64, minMemory, maxMemory int64) Limit
// WithStreamLimit creates a copy of this limit object, with stream limits adjusted
// as specified.
2022-01-15 02:10:18 +08:00
WithStreamLimit(numStreamsIn, numStreamsOut, numStreams int) Limit
// WithConnLimit creates a copy of this limit object, with connetion limits adjusted
// as specified.
2022-01-15 02:10:18 +08:00
WithConnLimit(numConnsIn, numConnsOut, numConns int) Limit
// WithFDLimit creates a copy of this limit object, with file descriptor limits adjusted
// as specified
WithFDLimit(numFD int) Limit
2021-12-22 17:39:46 +08:00
}
2021-12-23 23:46:08 +08:00
2022-01-05 22:54:28 +08:00
// Limiter is the interface for providing limits to the resource manager.
2021-12-23 23:46:08 +08:00
type Limiter interface {
GetSystemLimits() Limit
GetTransientLimits() Limit
GetServiceLimits(svc string) Limit
GetServicePeerLimits(svc string) Limit
2021-12-23 23:46:08 +08:00
GetProtocolLimits(proto protocol.ID) Limit
GetProtocolPeerLimits(proto protocol.ID) Limit
2021-12-23 23:46:08 +08:00
GetPeerLimits(p peer.ID) Limit
GetStreamLimits(p peer.ID) Limit
GetConnLimits() Limit
}
2021-12-24 18:12:44 +08:00
2022-01-05 22:54:28 +08:00
// BasicLimiter is a limiter with fixed limits.
2021-12-24 18:12:44 +08:00
type BasicLimiter struct {
SystemLimits Limit
TransientLimits Limit
DefaultServiceLimits Limit
DefaultServicePeerLimits Limit
ServiceLimits map[string]Limit
ServicePeerLimits map[string]Limit
DefaultProtocolLimits Limit
DefaultProtocolPeerLimits Limit
ProtocolLimits map[protocol.ID]Limit
ProtocolPeerLimits map[protocol.ID]Limit
DefaultPeerLimits Limit
PeerLimits map[peer.ID]Limit
ConnLimits Limit
StreamLimits Limit
2021-12-24 18:12:44 +08:00
}
var _ Limiter = (*BasicLimiter)(nil)
2022-01-06 18:57:37 +08:00
// BaseLimit is a mixin type for basic resource limits.
type BaseLimit struct {
2022-01-15 02:10:18 +08:00
Streams int
2022-01-06 18:57:37 +08:00
StreamsInbound int
StreamsOutbound int
2022-01-15 02:10:18 +08:00
Conns int
2022-01-06 18:57:37 +08:00
ConnsInbound int
ConnsOutbound int
FD int
2022-01-05 22:54:28 +08:00
}
// MemoryLimit is a mixin type for memory limits
type MemoryLimit struct {
MemoryFraction float64
MinMemory int64
MaxMemory int64
}
2022-01-05 22:54:28 +08:00
func (l *BaseLimit) GetStreamLimit(dir network.Direction) int {
2021-12-24 18:12:44 +08:00
if dir == network.DirInbound {
return l.StreamsInbound
} else {
return l.StreamsOutbound
}
}
2022-01-15 02:10:18 +08:00
func (l *BaseLimit) GetStreamTotalLimit() int {
return l.Streams
}
2022-01-05 22:54:28 +08:00
func (l *BaseLimit) GetConnLimit(dir network.Direction) int {
2021-12-24 18:12:44 +08:00
if dir == network.DirInbound {
return l.ConnsInbound
} else {
return l.ConnsOutbound
}
}
2022-01-15 02:10:18 +08:00
func (l *BaseLimit) GetConnTotalLimit() int {
return l.Conns
}
2022-01-05 22:54:28 +08:00
func (l *BaseLimit) GetFDLimit() int {
2021-12-24 18:12:44 +08:00
return l.FD
}
func (l *BasicLimiter) GetSystemLimits() Limit {
return l.SystemLimits
}
func (l *BasicLimiter) GetTransientLimits() Limit {
return l.TransientLimits
}
func (l *BasicLimiter) GetServiceLimits(svc string) Limit {
sl, ok := l.ServiceLimits[svc]
if !ok {
return l.DefaultServiceLimits
}
return sl
}
func (l *BasicLimiter) GetServicePeerLimits(svc string) Limit {
pl, ok := l.ServicePeerLimits[svc]
if !ok {
return l.DefaultServicePeerLimits
}
return pl
}
2021-12-24 18:12:44 +08:00
func (l *BasicLimiter) GetProtocolLimits(proto protocol.ID) Limit {
pl, ok := l.ProtocolLimits[proto]
if !ok {
return l.DefaultProtocolLimits
}
return pl
}
func (l *BasicLimiter) GetProtocolPeerLimits(proto protocol.ID) Limit {
pl, ok := l.ProtocolPeerLimits[proto]
if !ok {
return l.DefaultProtocolPeerLimits
}
return pl
}
2021-12-24 18:12:44 +08:00
func (l *BasicLimiter) GetPeerLimits(p peer.ID) Limit {
pl, ok := l.PeerLimits[p]
if !ok {
return l.DefaultPeerLimits
}
return pl
}
func (l *BasicLimiter) GetStreamLimits(p peer.ID) Limit {
return l.StreamLimits
}
func (l *BasicLimiter) GetConnLimits() Limit {
return l.ConnLimits
}
2022-01-06 18:57:37 +08:00
func (l *MemoryLimit) GetMemory(memoryCap int64) int64 {
return memoryLimit(memoryCap, l.MemoryFraction, l.MinMemory, l.MaxMemory)
2022-01-06 18:57:37 +08:00
}
2022-01-13 21:37:02 +08:00
func memoryLimit(memoryCap int64, memFraction float64, minMemory, maxMemory int64) int64 {
memoryCap = int64(float64(memoryCap) * memFraction)
2022-01-13 21:37:02 +08:00
switch {
case memoryCap < minMemory:
return minMemory
case memoryCap > maxMemory:
return maxMemory
default:
return memoryCap
}
}