mirror of
https://github.com/libp2p/go-libp2p-resource-manager.git
synced 2025-03-31 22:50:19 +08:00
normalize limiter constructors
This commit is contained in:
parent
9d5e7928e1
commit
859d2061ea
@ -30,6 +30,21 @@ type DefaultLimitConfig struct {
|
|||||||
StreamMemory int64
|
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.
|
// DefaultLimits are the limits used by the default limiter constructors.
|
||||||
var DefaultLimits = DefaultLimitConfig{
|
var DefaultLimits = DefaultLimitConfig{
|
||||||
SystemBaseLimit: BaseLimit{
|
SystemBaseLimit: BaseLimit{
|
||||||
|
@ -71,8 +71,8 @@ func (l *DynamicLimit) WithFDLimit(numFD int) Limit {
|
|||||||
|
|
||||||
// NewDefaultDynamicLimiter creates a limiter with default limits and a memory cap
|
// NewDefaultDynamicLimiter creates a limiter with default limits and a memory cap
|
||||||
// dynamically computed based on available memory.
|
// dynamically computed based on available memory.
|
||||||
func NewDefaultDynamicLimiter() *BasicLimiter {
|
func NewDefaultDynamicLimiter(memFraction float64, minMemory, maxMemory int64) *BasicLimiter {
|
||||||
return NewDynamicLimiter(DefaultLimits)
|
return NewDynamicLimiter(DefaultLimits.WithSystemMemory(memFraction, minMemory, maxMemory))
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDynamicLimiter crates a dynamic limiter with the specified defaults
|
// NewDynamicLimiter crates a dynamic limiter with the specified defaults
|
||||||
|
@ -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
|
// specified as a fraction of total system memory. The assigned memory will not be less than
|
||||||
// minMemory or more than maxMemory.
|
// minMemory or more than maxMemory.
|
||||||
func NewDefaultStaticLimiter(memFraction float64, minMemory, maxMemory int64) *BasicLimiter {
|
func NewDefaultStaticLimiter(memFraction float64, minMemory, maxMemory int64) *BasicLimiter {
|
||||||
memoryCap := memoryLimit(int64(memory.TotalMemory()), memFraction, minMemory, maxMemory)
|
return NewStaticLimiter(DefaultLimits.WithSystemMemory(memFraction, minMemory, maxMemory))
|
||||||
return NewStaticLimiter(memoryCap, DefaultLimits)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDefaultFixedLimiter creates a static limiter with default base limits and a specified system
|
// NewDefaultFixedLimiter creates a static limiter with default base limits and a specified system
|
||||||
// memory cap.
|
// memory cap.
|
||||||
func NewDefaultFixedLimiter(memoryCap int64) *BasicLimiter {
|
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
|
// NewDefaultLimiter creates a static limiter with the default limits
|
||||||
func NewDefaultLimiter() *BasicLimiter {
|
func NewDefaultLimiter() *BasicLimiter {
|
||||||
return NewDefaultStaticLimiter(
|
return NewStaticLimiter(DefaultLimits)
|
||||||
DefaultLimits.SystemMemory.MemoryFraction,
|
|
||||||
DefaultLimits.SystemMemory.MinMemory,
|
|
||||||
DefaultLimits.SystemMemory.MaxMemory,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStaticLimiter creates a static limiter using the specified system memory cap and default
|
// NewStaticLimiter creates a static limiter using the specified system memory cap and default
|
||||||
// limit config.
|
// 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{
|
system := &StaticLimit{
|
||||||
Memory: memoryCap,
|
Memory: memoryCap,
|
||||||
BaseLimit: cfg.SystemBaseLimit,
|
BaseLimit: cfg.SystemBaseLimit,
|
||||||
}
|
}
|
||||||
transient := &StaticLimit{
|
transient := &StaticLimit{
|
||||||
Memory: cfg.SystemMemory.GetMemory(memoryCap),
|
Memory: cfg.TransientMemory.GetMemory(memoryCap),
|
||||||
BaseLimit: cfg.TransientBaseLimit,
|
BaseLimit: cfg.TransientBaseLimit,
|
||||||
}
|
}
|
||||||
svc := &StaticLimit{
|
svc := &StaticLimit{
|
||||||
|
Loading…
Reference in New Issue
Block a user