Skip to content

Commit

Permalink
normalize limiter constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Jan 17, 2022
1 parent 0ef82d9 commit 1a383bd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
15 changes: 15 additions & 0 deletions p2p/host/resource-manager/limit_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ type DefaultLimitConfig struct {
StreamMemory int64
}

func (cfg *DefaultLimitConfig) WithSystemMemory(memFraction float64, minMemory, maxMemory int64) DefaultLimitConfig {
refactor := memFraction / cfg.SystemMemory.MemoryFraction
r := *cfg
r.SystemMemory.MemoryFraction = memFraction
r.SystemMemory.MinMemory = minMemory
r.SystemMemory.MaxMemory = maxMemory
r.TransientMemory.MemoryFraction *= refactor
r.ServiceMemory.MemoryFraction *= refactor
r.ServicePeerMemory.MemoryFraction *= refactor
r.ProtocolMemory.MemoryFraction *= refactor
r.ProtocolPeerMemory.MemoryFraction *= refactor
r.PeerMemory.MemoryFraction *= refactor
return r
}

// DefaultLimits are the limits used by the default limiter constructors.
var DefaultLimits = DefaultLimitConfig{
SystemBaseLimit: BaseLimit{
Expand Down
4 changes: 2 additions & 2 deletions p2p/host/resource-manager/limit_dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func (l *DynamicLimit) WithFDLimit(numFD int) Limit {

// NewDefaultDynamicLimiter creates a limiter with default limits and a memory cap
// dynamically computed based on available memory.
func NewDefaultDynamicLimiter() *BasicLimiter {
return NewDynamicLimiter(DefaultLimits)
func NewDefaultDynamicLimiter(memFraction float64, minMemory, maxMemory int64) *BasicLimiter {
return NewDynamicLimiter(DefaultLimits.WithSystemMemory(memFraction, minMemory, maxMemory))
}

// NewDynamicLimiter crates a dynamic limiter with the specified defaults
Expand Down
20 changes: 10 additions & 10 deletions p2p/host/resource-manager/limit_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,34 @@ func (l *StaticLimit) WithFDLimit(numFD int) Limit {
// specified as a fraction of total system memory. The assigned memory will not be less than
// minMemory or more than maxMemory.
func NewDefaultStaticLimiter(memFraction float64, minMemory, maxMemory int64) *BasicLimiter {
memoryCap := memoryLimit(int64(memory.TotalMemory()), memFraction, minMemory, maxMemory)
return NewStaticLimiter(memoryCap, DefaultLimits)
return NewStaticLimiter(DefaultLimits.WithSystemMemory(memFraction, minMemory, maxMemory))
}

// NewDefaultFixedLimiter creates a static limiter with default base limits and a specified system
// memory cap.
func NewDefaultFixedLimiter(memoryCap int64) *BasicLimiter {
return NewStaticLimiter(memoryCap, DefaultLimits)
return NewStaticLimiter(DefaultLimits.WithSystemMemory(1, memoryCap, memoryCap))
}

// NewDefaultLimiter creates a static limiter with the default limits
func NewDefaultLimiter() *BasicLimiter {
return NewDefaultStaticLimiter(
DefaultLimits.SystemMemory.MemoryFraction,
DefaultLimits.SystemMemory.MinMemory,
DefaultLimits.SystemMemory.MaxMemory,
)
return NewStaticLimiter(DefaultLimits)
}

// NewStaticLimiter creates a static limiter using the specified system memory cap and default
// limit config.
func NewStaticLimiter(memoryCap int64, cfg DefaultLimitConfig) *BasicLimiter {
func NewStaticLimiter(cfg DefaultLimitConfig) *BasicLimiter {
memoryCap := memoryLimit(
int64(memory.TotalMemory()),
cfg.SystemMemory.MemoryFraction,
cfg.SystemMemory.MinMemory,
cfg.SystemMemory.MaxMemory)
system := &StaticLimit{
Memory: memoryCap,
BaseLimit: cfg.SystemBaseLimit,
}
transient := &StaticLimit{
Memory: cfg.SystemMemory.GetMemory(memoryCap),
Memory: cfg.TransientMemory.GetMemory(memoryCap),
BaseLimit: cfg.TransientBaseLimit,
}
svc := &StaticLimit{
Expand Down

0 comments on commit 1a383bd

Please sign in to comment.