Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
implement scaling of limits
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jun 10, 2022
1 parent c17d236 commit da2f584
Show file tree
Hide file tree
Showing 8 changed files with 537 additions and 877 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.17
require (
github.com/ipfs/go-log/v2 v2.5.0
github.com/libp2p/go-libp2p-core v0.14.0
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
)

require (
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXS
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
120 changes: 46 additions & 74 deletions limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ type Limit interface {
GetConnTotalLimit() int
// GetFDLimit returns the file descriptor limit.
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.
WithStreamLimit(numStreamsIn, numStreamsOut, numStreams int) Limit
// WithConnLimit creates a copy of this limit object, with connetion limits adjusted
// as specified.
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
}

// Limiter is the interface for providing limits to the resource manager.
Expand All @@ -48,25 +35,16 @@ type Limiter interface {
GetConnLimits() Limit
}

// BasicLimiter is a limiter with fixed limits.
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
// fixedLimiter is a limiter with fixed limits.
type fixedLimiter struct {
LimitConfig
}

var _ Limiter = (*BasicLimiter)(nil)
var _ Limiter = (*fixedLimiter)(nil)

func NewFixedLimiter(conf *LimitConfig) Limiter {
return &fixedLimiter{*conf}
}

// BaseLimit is a mixin type for basic resource limits.
type BaseLimit struct {
Expand All @@ -77,13 +55,19 @@ type BaseLimit struct {
ConnsInbound int
ConnsOutbound int
FD int
Memory int64
}

// MemoryLimit is a mixin type for memory limits
type MemoryLimit struct {
MemoryFraction float64
MinMemory int64
MaxMemory int64
// BaseLimitIncrease is the increase per GB of system memory.
type BaseLimitIncrease struct {
Streams int
StreamsInbound int
StreamsOutbound int
Conns int
ConnsInbound int
ConnsOutbound int
Memory int64
FDFraction float64
}

func (l *BaseLimit) GetStreamLimit(dir network.Direction) int {
Expand Down Expand Up @@ -114,74 +98,62 @@ func (l *BaseLimit) GetFDLimit() int {
return l.FD
}

func (l *BasicLimiter) GetSystemLimits() Limit {
return l.SystemLimits
func (l *BaseLimit) GetMemoryLimit() int64 {
return l.Memory
}

func (l *fixedLimiter) GetSystemLimits() Limit {
return &l.SystemLimit
}

func (l *BasicLimiter) GetTransientLimits() Limit {
return l.TransientLimits
func (l *fixedLimiter) GetTransientLimits() Limit {
return &l.TransientLimit
}

func (l *BasicLimiter) GetServiceLimits(svc string) Limit {
func (l *fixedLimiter) GetServiceLimits(svc string) Limit {
sl, ok := l.ServiceLimits[svc]
if !ok {
return l.DefaultServiceLimits
return &l.DefaultServiceLimit
}
return sl
return &sl
}

func (l *BasicLimiter) GetServicePeerLimits(svc string) Limit {
func (l *fixedLimiter) GetServicePeerLimits(svc string) Limit {
pl, ok := l.ServicePeerLimits[svc]
if !ok {
return l.DefaultServicePeerLimits
return &l.DefaultServicePeerLimit
}
return pl
return &pl
}

func (l *BasicLimiter) GetProtocolLimits(proto protocol.ID) Limit {
func (l *fixedLimiter) GetProtocolLimits(proto protocol.ID) Limit {
pl, ok := l.ProtocolLimits[proto]
if !ok {
return l.DefaultProtocolLimits
return &l.DefaultProtocolLimit
}
return pl
return &pl
}

func (l *BasicLimiter) GetProtocolPeerLimits(proto protocol.ID) Limit {
func (l *fixedLimiter) GetProtocolPeerLimits(proto protocol.ID) Limit {
pl, ok := l.ProtocolPeerLimits[proto]
if !ok {
return l.DefaultProtocolPeerLimits
return &l.DefaultProtocolPeerLimit
}
return pl
return &pl
}

func (l *BasicLimiter) GetPeerLimits(p peer.ID) Limit {
func (l *fixedLimiter) GetPeerLimits(p peer.ID) Limit {
pl, ok := l.PeerLimits[p]
if !ok {
return l.DefaultPeerLimits
return &l.DefaultPeerLimit
}
return pl
}

func (l *BasicLimiter) GetStreamLimits(p peer.ID) Limit {
return l.StreamLimits
}

func (l *BasicLimiter) GetConnLimits() Limit {
return l.ConnLimits
return &pl
}

func (l *MemoryLimit) GetMemory(memoryCap int64) int64 {
return memoryLimit(memoryCap, l.MemoryFraction, l.MinMemory, l.MaxMemory)
func (l *fixedLimiter) GetStreamLimits(_ peer.ID) Limit {
return &l.StreamLimit
}

func memoryLimit(memoryCap int64, memFraction float64, minMemory, maxMemory int64) int64 {
memoryCap = int64(float64(memoryCap) * memFraction)
switch {
case memoryCap < minMemory:
return minMemory
case memoryCap > maxMemory:
return maxMemory
default:
return memoryCap
}
func (l *fixedLimiter) GetConnLimits() Limit {
return &l.ConnLimit
}
Loading

0 comments on commit da2f584

Please sign in to comment.