mirror of
https://github.com/libp2p/go-libp2p-resource-manager.git
synced 2025-01-27 12:50:07 +08:00
normalize limiter constructors
This commit is contained in:
parent
9d5e7928e1
commit
859d2061ea
@ -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{
|
||||
|
@ -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
|
||||
|
@ -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{
|
||||
|
Loading…
Reference in New Issue
Block a user