From 859d2061ea0d4b96743ab346e1f8923a84c1c0a7 Mon Sep 17 00:00:00 2001 From: vyzo Date: Mon, 17 Jan 2022 11:17:11 +0200 Subject: [PATCH] normalize limiter constructors --- limit_defaults.go | 15 +++++++++++++++ limit_dynamic.go | 4 ++-- limit_static.go | 20 ++++++++++---------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/limit_defaults.go b/limit_defaults.go index f9024d7..8d5e3cd 100644 --- a/limit_defaults.go +++ b/limit_defaults.go @@ -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{ diff --git a/limit_dynamic.go b/limit_dynamic.go index ad4b14c..e804492 100644 --- a/limit_dynamic.go +++ b/limit_dynamic.go @@ -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 diff --git a/limit_static.go b/limit_static.go index 3c44956..e96884a 100644 --- a/limit_static.go +++ b/limit_static.go @@ -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{