mirror of
https://github.com/libp2p/go-libp2p-resource-manager.git
synced 2025-01-28 05:10:26 +08:00
add fluid copy constructors for limits
so that we can easily clone and adjust limits.
This commit is contained in:
parent
2648ba9f19
commit
05a6764a69
17
limit.go
17
limit.go
@ -8,10 +8,27 @@ import (
|
|||||||
|
|
||||||
// Limit is an object that specifies basic resource limits.
|
// Limit is an object that specifies basic resource limits.
|
||||||
type Limit interface {
|
type Limit interface {
|
||||||
|
// GetMemoryLimit returns the (current) memory limit.
|
||||||
GetMemoryLimit() int64
|
GetMemoryLimit() int64
|
||||||
|
// GetStreamLimit returns the stream limit, for inbound or outbound streams.
|
||||||
GetStreamLimit(network.Direction) int
|
GetStreamLimit(network.Direction) int
|
||||||
|
// GetConnLimit returns the connection limit, for inbound or outbound connections.
|
||||||
GetConnLimit(network.Direction) int
|
GetConnLimit(network.Direction) int
|
||||||
|
// GetFDLimit returns the file descriptor limit.
|
||||||
GetFDLimit() int
|
GetFDLimit() int
|
||||||
|
|
||||||
|
// WithMemoryLimit creates a copy of this limit object, with memory limit adjusted to
|
||||||
|
// the specified memFraction of its current value, bounded by minMemory and maxMemory.
|
||||||
|
WithMemoryLimit(memFraction float64, minMemory, maxMemory int64) Limit
|
||||||
|
// WithStreamLimit creates a copy of this limit object, with stream limits adjusted
|
||||||
|
// as specified.
|
||||||
|
WithStreamLimit(numStreamsIn, numStreamsOut int) Limit
|
||||||
|
// WithConnLimit creates a copy of this limit object, with connetion limits adjusted
|
||||||
|
// as specified.
|
||||||
|
WithConnLimit(numConnsIn, numConnsOut int) Limit
|
||||||
|
// WithFDLimit creates a copy of this limit object, with file descriptor limits adjusted
|
||||||
|
// as specified
|
||||||
|
WithFDLimit(numFD int) Limit
|
||||||
}
|
}
|
||||||
|
|
||||||
// Limiter is the interface for providing limits to the resource manager.
|
// Limiter is the interface for providing limits to the resource manager.
|
||||||
|
@ -38,6 +38,46 @@ func (l *DynamicLimit) GetMemoryLimit() int64 {
|
|||||||
return limit
|
return limit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *DynamicLimit) WithMemoryLimit(memFraction float64, minMemory, maxMemory int64) Limit {
|
||||||
|
r := new(DynamicLimit)
|
||||||
|
*r = *l
|
||||||
|
|
||||||
|
r.MemoryFraction *= memFraction
|
||||||
|
r.MinMemory = minMemory
|
||||||
|
r.MaxMemory = maxMemory
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *DynamicLimit) WithStreamLimit(numStreamsIn, numStreamsOut int) Limit {
|
||||||
|
r := new(DynamicLimit)
|
||||||
|
*r = *l
|
||||||
|
|
||||||
|
r.BaseLimit.StreamsInbound = numStreamsIn
|
||||||
|
r.BaseLimit.StreamsOutbound = numStreamsOut
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *DynamicLimit) WithConnLimit(numConnsIn, numConnsOut int) Limit {
|
||||||
|
r := new(DynamicLimit)
|
||||||
|
*r = *l
|
||||||
|
|
||||||
|
r.BaseLimit.ConnsInbound = numConnsIn
|
||||||
|
r.BaseLimit.ConnsOutbound = numConnsOut
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *DynamicLimit) WithFDLimit(numFD int) Limit {
|
||||||
|
r := new(DynamicLimit)
|
||||||
|
*r = *l
|
||||||
|
|
||||||
|
r.BaseLimit.FD = numFD
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
// NewDynamicLimiter creates a limiter with default limits and a memory cap dynamically computed
|
// NewDynamicLimiter creates a limiter with default limits and a memory cap dynamically computed
|
||||||
// based on available memory. minMemory and maxMemory specify the system memory bounds,
|
// based on available memory. minMemory and maxMemory specify the system memory bounds,
|
||||||
// while memFraction specifies the fraction of available memory available for the system, within
|
// while memFraction specifies the fraction of available memory available for the system, within
|
||||||
|
@ -16,6 +16,49 @@ func (l *StaticLimit) GetMemoryLimit() int64 {
|
|||||||
return l.Memory
|
return l.Memory
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *StaticLimit) WithMemoryLimit(memFraction float64, minMemory, maxMemory int64) Limit {
|
||||||
|
r := new(StaticLimit)
|
||||||
|
*r = *l
|
||||||
|
|
||||||
|
r.Memory = int64(memFraction * float64(r.Memory))
|
||||||
|
if r.Memory < minMemory {
|
||||||
|
r.Memory = minMemory
|
||||||
|
} else if r.Memory > maxMemory {
|
||||||
|
r.Memory = maxMemory
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *StaticLimit) WithStreamLimit(numStreamsIn, numStreamsOut int) Limit {
|
||||||
|
r := new(StaticLimit)
|
||||||
|
*r = *l
|
||||||
|
|
||||||
|
r.BaseLimit.StreamsInbound = numStreamsIn
|
||||||
|
r.BaseLimit.StreamsOutbound = numStreamsOut
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *StaticLimit) WithConnLimit(numConnsIn, numConnsOut int) Limit {
|
||||||
|
r := new(StaticLimit)
|
||||||
|
*r = *l
|
||||||
|
|
||||||
|
r.BaseLimit.ConnsInbound = numConnsIn
|
||||||
|
r.BaseLimit.ConnsOutbound = numConnsOut
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *StaticLimit) WithFDLimit(numFD int) Limit {
|
||||||
|
r := new(StaticLimit)
|
||||||
|
*r = *l
|
||||||
|
|
||||||
|
r.BaseLimit.FD = numFD
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
// NewStaticLimiter creates a limiter with default base limits and a system memory cap specified as
|
// NewStaticLimiter creates a limiter with default base limits and a system memory cap specified as
|
||||||
// a fraction of total system memory. The assigned memory will not be less than minMemory or more
|
// a fraction of total system memory. The assigned memory will not be less than minMemory or more
|
||||||
// than maxMemory.
|
// than maxMemory.
|
||||||
|
Loading…
Reference in New Issue
Block a user