Documentation
¶
Overview ¶
Package memlimit configures GOMEMLIMIT from a memory limit provider.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoCgroup is returned when the process is not assigned to a cgroup. ErrNoCgroup = errors.New("process is not in cgroup") // ErrCgroupsNotSupported is returned when the system does not support cgroups. ErrCgroupsNotSupported = errors.New("cgroups is not supported on this system") )
var ErrNoLimit = errors.New("memory is not limited")
ErrNoLimit indicates that a Provider reports no memory limit. Set handles it as success by setting GOMEMLIMIT to math.MaxInt64.
Functions ¶
func FromCgroup ¶
FromCgroup returns the memory limit for the current process's cgroup.
func FromSystem ¶ added in v0.5.0
FromSystem returns the total system memory.
func Set ¶ added in v1.0.0
Set sets GOMEMLIMIT using the configured provider.
By default, Set uses 90% of the limit reported by FromCgroup. AUTOMEMLIMIT overrides the configured ratio with a value in (0.0, 1.0]. Setting it to "off" disables Set.
If the GOMEMLIMIT environment variable is present or AUTOMEMLIMIT is set to "off", Set returns the current GOMEMLIMIT without calling the provider or starting a refresh goroutine.
Set returns the resulting GOMEMLIMIT on success. On error, it returns the previous GOMEMLIMIT and the error. It handles ErrNoLimit as success by setting GOMEMLIMIT to math.MaxInt64.
Types ¶
type Option ¶ added in v0.3.0
type Option func(cfg *config)
Option configures the behavior of Set.
func WithLogger ¶ added in v0.6.0
WithLogger configures the logger. It adds the "package" attribute to every log record.
Default: logging disabled
func WithMin ¶ added in v1.0.0
WithMin configures the minimum GOMEMLIMIT after applying the ratio. A non-positive value disables the minimum.
Default: 0 (no minimum)
func WithProvider ¶ added in v0.3.0
WithProvider configures the provider used by Set.
Default: FromCgroup
func WithRatio ¶ added in v0.3.0
WithRatio configures the fraction of the memory limit used for GOMEMLIMIT. The ratio must be in (0.0, 1.0].
Default: 0.9
func WithRefreshInterval ¶ added in v0.7.0
WithRefreshInterval configures Set to periodically refresh GOMEMLIMIT.
Set starts a refresh goroutine after the initial provider call when refresh is positive and ctx is non-nil. The goroutine starts even if the initial call returns an error. Canceling ctx stops the refresh loop but does not interrupt an active provider call. Provider errors other than ErrNoLimit are reported to the configured logger and do not stop later refreshes.
Default: 0 (no refresh)
type Provider ¶
Provider returns a memory limit in bytes. It should return ErrNoLimit when the source reports no limit.
func ApplyFallback ¶ added in v0.4.0
ApplyFallback returns a Provider that calls fallback when provider returns an error, including ErrNoLimit.
func ApplyRatio ¶ added in v0.4.0
ApplyRatio wraps provider and applies ratio to its result. The ratio must be in (0.0, 1.0].