memlimit

package
v1.0.0-pre.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 15 Imported by: 96

Documentation

Overview

Package memlimit configures GOMEMLIMIT from a memory limit provider.

Index

Constants

This section is empty.

Variables

View Source
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")
)
View Source
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

func FromCgroup() (uint64, error)

FromCgroup returns the memory limit for the current process's cgroup.

func FromSystem added in v0.5.0

func FromSystem() (uint64, error)

FromSystem returns the total system memory.

func Set added in v1.0.0

func Set(opts ...Option) (_limit int64, _err error)

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

func WithLogger(logger *slog.Logger) Option

WithLogger configures the logger. It adds the "package" attribute to every log record.

Default: logging disabled

func WithMin added in v1.0.0

func WithMin(minLimit int64) Option

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

func WithProvider(provider Provider) Option

WithProvider configures the provider used by Set.

Default: FromCgroup

func WithRatio added in v0.3.0

func WithRatio(ratio float64) Option

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

func WithRefreshInterval(ctx context.Context, refresh time.Duration) Option

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

type Provider func() (uint64, error)

Provider returns a memory limit in bytes. It should return ErrNoLimit when the source reports no limit.

func ApplyFallback added in v0.4.0

func ApplyFallback(provider Provider, fallback Provider) Provider

ApplyFallback returns a Provider that calls fallback when provider returns an error, including ErrNoLimit.

func ApplyRatio added in v0.4.0

func ApplyRatio(provider Provider, ratio float64) Provider

ApplyRatio wraps provider and applies ratio to its result. The ratio must be in (0.0, 1.0].

func Limit

func Limit(limit uint64) Provider

Limit returns a Provider that always returns limit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL