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"
|
2022-01-05 17:04:11 +08:00
|
|
|
|
2022-01-05 22:54:28 +08:00
|
|
|
"github.com/elastic/gosigar"
|
2022-01-05 17:04:11 +08:00
|
|
|
"github.com/pbnjay/memory"
|
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() int64
|
2021-12-23 21:28:14 +08:00
|
|
|
GetStreamLimit(network.Direction) int
|
|
|
|
GetConnLimit(network.Direction) int
|
|
|
|
GetFDLimit() int
|
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
|
|
|
|
GetProtocolLimits(proto protocol.ID) Limit
|
|
|
|
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
|
|
|
type BaseLimit struct {
|
2021-12-24 18:12:44 +08:00
|
|
|
StreamsInbound int
|
|
|
|
StreamsOutbound int
|
|
|
|
ConnsInbound int
|
|
|
|
ConnsOutbound int
|
|
|
|
FD int
|
|
|
|
}
|
|
|
|
|
2022-01-05 22:54:28 +08:00
|
|
|
// StaticLimit is a limit with static values.
|
|
|
|
type StaticLimit struct {
|
|
|
|
BaseLimit
|
|
|
|
Memory int64
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:12:44 +08:00
|
|
|
var _ Limit = (*StaticLimit)(nil)
|
|
|
|
|
2022-01-05 22:54:28 +08:00
|
|
|
// DynamicLimit is a limit with dynamic memory values, based on available memory
|
|
|
|
type DynamicLimit struct {
|
|
|
|
BaseLimit
|
|
|
|
|
|
|
|
// MinMemory is the minimum memory for this limit
|
|
|
|
MinMemory int64
|
|
|
|
// MaxMemory is the maximum memory for this limit
|
|
|
|
MaxMemory int64
|
|
|
|
// MemoryFraction is the fraction of available memory allowed for this limit,
|
|
|
|
// bounded by [MinMemory, MaxMemory]
|
|
|
|
MemoryFraction int
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Limit = (*DynamicLimit)(nil)
|
|
|
|
|
|
|
|
// BasicLimiter is a limiter with fixed limits.
|
2021-12-24 18:12:44 +08:00
|
|
|
type BasicLimiter struct {
|
|
|
|
SystemLimits Limit
|
|
|
|
TransientLimits Limit
|
|
|
|
DefaultServiceLimits Limit
|
|
|
|
ServiceLimits map[string]Limit
|
|
|
|
DefaultProtocolLimits Limit
|
|
|
|
ProtocolLimits map[protocol.ID]Limit
|
|
|
|
DefaultPeerLimits Limit
|
|
|
|
PeerLimits map[peer.ID]Limit
|
|
|
|
ConnLimits Limit
|
|
|
|
StreamLimits Limit
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Limiter = (*BasicLimiter)(nil)
|
|
|
|
|
2022-01-05 22:54:28 +08:00
|
|
|
// NewStaticLimiter creates a limiter with default limits and a system memory cap; if the
|
|
|
|
// system memory cap is 0, then 1/8th of the total memory is used.
|
|
|
|
func NewStaticLimiter(memoryCap int64) *BasicLimiter {
|
2022-01-05 17:04:11 +08:00
|
|
|
if memoryCap == 0 {
|
|
|
|
memoryCap = int64(memory.TotalMemory() / 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
system := &StaticLimit{
|
2022-01-05 22:54:28 +08:00
|
|
|
Memory: memoryCap,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 4096,
|
|
|
|
StreamsOutbound: 16384,
|
|
|
|
ConnsInbound: 256,
|
|
|
|
ConnsOutbound: 512,
|
|
|
|
FD: 512,
|
|
|
|
},
|
2022-01-05 17:04:11 +08:00
|
|
|
}
|
|
|
|
transient := &StaticLimit{
|
2022-01-05 22:54:28 +08:00
|
|
|
Memory: memoryCap / 16,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 128,
|
|
|
|
StreamsOutbound: 512,
|
|
|
|
ConnsInbound: 32,
|
|
|
|
ConnsOutbound: 128,
|
|
|
|
FD: 128,
|
|
|
|
},
|
2022-01-05 17:04:11 +08:00
|
|
|
}
|
|
|
|
svc := &StaticLimit{
|
2022-01-05 22:54:28 +08:00
|
|
|
Memory: memoryCap / 2,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 2048,
|
|
|
|
StreamsOutbound: 8192,
|
|
|
|
},
|
2022-01-05 17:04:11 +08:00
|
|
|
}
|
|
|
|
proto := &StaticLimit{
|
2022-01-05 22:54:28 +08:00
|
|
|
Memory: memoryCap / 4,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 1024,
|
|
|
|
StreamsOutbound: 4096,
|
|
|
|
},
|
2022-01-05 17:04:11 +08:00
|
|
|
}
|
|
|
|
peer := &StaticLimit{
|
2022-01-05 22:54:28 +08:00
|
|
|
Memory: memoryCap / 16,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 512,
|
|
|
|
StreamsOutbound: 2048,
|
|
|
|
ConnsInbound: 8,
|
|
|
|
ConnsOutbound: 16,
|
|
|
|
FD: 8,
|
|
|
|
},
|
2022-01-05 17:04:11 +08:00
|
|
|
}
|
|
|
|
conn := &StaticLimit{
|
2022-01-05 22:54:28 +08:00
|
|
|
Memory: 16 << 20,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
ConnsInbound: 1,
|
|
|
|
ConnsOutbound: 1,
|
|
|
|
FD: 1,
|
|
|
|
},
|
2022-01-05 17:04:11 +08:00
|
|
|
}
|
|
|
|
stream := &StaticLimit{
|
2022-01-05 22:54:28 +08:00
|
|
|
Memory: 16 << 20,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 1,
|
|
|
|
StreamsOutbound: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BasicLimiter{
|
|
|
|
SystemLimits: system,
|
|
|
|
TransientLimits: transient,
|
|
|
|
DefaultServiceLimits: svc,
|
|
|
|
DefaultProtocolLimits: proto,
|
|
|
|
DefaultPeerLimits: peer,
|
|
|
|
ConnLimits: conn,
|
|
|
|
StreamLimits: stream,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDynamicLimiter creates a limiter with default limits and a memory cap dynamically computed
|
|
|
|
// based on available memory. minMemory and maxMemory specify the system memory bounds,
|
|
|
|
// while memFraction specifies the fraction of available memory available for the system, within
|
|
|
|
// the specified bounds.
|
|
|
|
func NewDynamicLimiter(minMemory, maxMemory int64, memFraction int) *BasicLimiter {
|
|
|
|
system := &DynamicLimit{
|
|
|
|
MinMemory: minMemory,
|
|
|
|
MaxMemory: maxMemory,
|
|
|
|
MemoryFraction: memFraction,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 4096,
|
|
|
|
StreamsOutbound: 16384,
|
|
|
|
ConnsInbound: 256,
|
|
|
|
ConnsOutbound: 512,
|
|
|
|
FD: 512,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
transient := &DynamicLimit{
|
|
|
|
MinMemory: minMemory / 16,
|
|
|
|
MaxMemory: maxMemory / 16,
|
|
|
|
MemoryFraction: memFraction * 16,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 128,
|
|
|
|
StreamsOutbound: 512,
|
|
|
|
ConnsInbound: 32,
|
|
|
|
ConnsOutbound: 128,
|
|
|
|
FD: 128,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
svc := &DynamicLimit{
|
|
|
|
MinMemory: minMemory / 2,
|
|
|
|
MaxMemory: maxMemory / 2,
|
|
|
|
MemoryFraction: memFraction * 2,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 2048,
|
|
|
|
StreamsOutbound: 8192,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
proto := &DynamicLimit{
|
|
|
|
MinMemory: minMemory / 4,
|
|
|
|
MaxMemory: maxMemory / 4,
|
|
|
|
MemoryFraction: memFraction * 4,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 1024,
|
|
|
|
StreamsOutbound: 4096,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
peer := &DynamicLimit{
|
|
|
|
MinMemory: minMemory / 16,
|
|
|
|
MaxMemory: maxMemory / 16,
|
|
|
|
MemoryFraction: memFraction * 16,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 512,
|
|
|
|
StreamsOutbound: 2048,
|
|
|
|
ConnsInbound: 8,
|
|
|
|
ConnsOutbound: 16,
|
|
|
|
FD: 8,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
conn := &StaticLimit{
|
|
|
|
Memory: 16 << 20,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
ConnsInbound: 1,
|
|
|
|
ConnsOutbound: 1,
|
|
|
|
FD: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
stream := &StaticLimit{
|
|
|
|
Memory: 16 << 20,
|
|
|
|
BaseLimit: BaseLimit{
|
|
|
|
StreamsInbound: 1,
|
|
|
|
StreamsOutbound: 1,
|
|
|
|
},
|
2022-01-05 17:04:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return &BasicLimiter{
|
|
|
|
SystemLimits: system,
|
|
|
|
TransientLimits: transient,
|
|
|
|
DefaultServiceLimits: svc,
|
|
|
|
DefaultProtocolLimits: proto,
|
|
|
|
DefaultPeerLimits: peer,
|
|
|
|
ConnLimits: conn,
|
|
|
|
StreamLimits: stream,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:12:44 +08:00
|
|
|
func (l *StaticLimit) GetMemoryLimit() int64 {
|
|
|
|
return l.Memory
|
|
|
|
}
|
|
|
|
|
2022-01-05 22:54:28 +08:00
|
|
|
func (l *DynamicLimit) GetMemoryLimit() int64 {
|
|
|
|
var mem gosigar.Mem
|
|
|
|
if err := mem.Get(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
limit := int64(mem.ActualFree) / int64(l.MemoryFraction)
|
|
|
|
if limit < l.MinMemory {
|
|
|
|
limit = l.MinMemory
|
|
|
|
} else if limit > l.MaxMemory {
|
|
|
|
limit = l.MaxMemory
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-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) GetProtocolLimits(proto protocol.ID) Limit {
|
|
|
|
pl, ok := l.ProtocolLimits[proto]
|
|
|
|
if !ok {
|
|
|
|
return l.DefaultProtocolLimits
|
|
|
|
}
|
|
|
|
return pl
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|