diff --git a/limit.go b/limit.go index f7335c3..5e93f09 100644 --- a/limit.go +++ b/limit.go @@ -198,3 +198,14 @@ func StreamBaseLimit() BaseLimit { StreamsOutbound: 1, } } + +func memoryLimit(memoryCap int64, minMemory, maxMemory int64) int64 { + switch { + case memoryCap < minMemory: + return minMemory + case memoryCap > maxMemory: + return maxMemory + default: + return memoryCap + } +} diff --git a/limit_dynamic.go b/limit_dynamic.go index 4dee2aa..174777a 100644 --- a/limit_dynamic.go +++ b/limit_dynamic.go @@ -33,13 +33,7 @@ func (l *DynamicLimit) GetMemoryLimit() int64 { freemem += (memstat.HeapInuse - memstat.HeapAlloc) + (memstat.HeapIdle - memstat.HeapReleased) limit := int64(float64(freemem) * l.MemoryFraction) - if limit < l.MinMemory { - limit = l.MinMemory - } else if limit > l.MaxMemory { - limit = l.MaxMemory - } - - return limit + return memoryLimit(limit, l.MinMemory, l.MaxMemory) } func (l *DynamicLimit) WithMemoryLimit(memFraction float64, minMemory, maxMemory int64) Limit { @@ -94,31 +88,31 @@ func NewDynamicLimiter(memFraction float64, minMemory, maxMemory int64) *BasicLi BaseLimit: DefaultSystemBaseLimit(), } transient := &DynamicLimit{ - MinMemory: minMemory / 4, - MaxMemory: maxMemory / 4, - MemoryFraction: memFraction / 4, + MinMemory: 64 << 20, + MaxMemory: 128 << 20, + MemoryFraction: memFraction / 16, BaseLimit: DefaultTransientBaseLimit(), } svc := &DynamicLimit{ - MinMemory: minMemory / 4, - MaxMemory: maxMemory / 4, + MinMemory: 64 << 20, + MaxMemory: 512 << 20, MemoryFraction: memFraction / 4, BaseLimit: DefaultServiceBaseLimit(), } proto := &DynamicLimit{ - MinMemory: minMemory / 4, - MaxMemory: maxMemory / 4, - MemoryFraction: memFraction / 4, + MinMemory: 64 << 20, + MaxMemory: 128 << 20, + MemoryFraction: memFraction / 16, BaseLimit: DefaultProtocolBaseLimit(), } peer := &DynamicLimit{ - MinMemory: minMemory / 4, - MaxMemory: maxMemory / 4, - MemoryFraction: memFraction / 4, + MinMemory: 64 << 20, + MaxMemory: 128 << 20, + MemoryFraction: memFraction / 16, BaseLimit: DefaultPeerBaseLimit(), } conn := &StaticLimit{ - Memory: 16 << 20, + Memory: 1 << 20, BaseLimit: ConnBaseLimit(), } stream := &StaticLimit{ diff --git a/limit_static.go b/limit_static.go index aa96bf7..d668a36 100644 --- a/limit_static.go +++ b/limit_static.go @@ -63,13 +63,7 @@ func (l *StaticLimit) WithFDLimit(numFD int) Limit { // a fraction of total system memory. The assigned memory will not be less than minMemory or more // than maxMemory. func NewStaticLimiter(memFraction float64, minMemory, maxMemory int64) *BasicLimiter { - memoryCap := int64(float64(memory.TotalMemory()) * memFraction) - switch { - case memoryCap < minMemory: - memoryCap = minMemory - case memoryCap > maxMemory: - memoryCap = maxMemory - } + memoryCap := memoryLimit(int64(float64(memory.TotalMemory())*memFraction), minMemory, maxMemory) return newDefaultStaticLimiter(memoryCap) } @@ -84,23 +78,23 @@ func newDefaultStaticLimiter(memoryCap int64) *BasicLimiter { BaseLimit: DefaultSystemBaseLimit(), } transient := &StaticLimit{ - Memory: memoryCap / 4, + Memory: memoryLimit(memoryCap/16, 64<<20, 128<<20), BaseLimit: DefaultTransientBaseLimit(), } svc := &StaticLimit{ - Memory: memoryCap / 4, + Memory: memoryLimit(memoryCap/4, 64<<20, 512<<20), BaseLimit: DefaultServiceBaseLimit(), } proto := &StaticLimit{ - Memory: memoryCap / 4, + Memory: memoryLimit(memoryCap/16, 64<<20, 128<<20), BaseLimit: DefaultProtocolBaseLimit(), } peer := &StaticLimit{ - Memory: memoryCap / 4, + Memory: memoryLimit(memoryCap/16, 64<<20, 128<<20), BaseLimit: DefaultPeerBaseLimit(), } conn := &StaticLimit{ - Memory: 16 << 20, + Memory: 1 << 20, BaseLimit: ConnBaseLimit(), } stream := &StaticLimit{