memlimit

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 12 Imported by: 96

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoLimit is returned when the memory limit is not set.
	ErrNoLimit = errors.New("memory is not limited")
	// ErrNoCgroup is returned when the process is not in 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")
)

Functions

func FromCgroup

func FromCgroup() (uint64, error)

FromCgroup returns the memory limit based on the cgroups version on this system.

func FromCgroupV1

func FromCgroupV1() (uint64, error)

FromCgroupV1 returns the memory limit from the cgroup v1.

func FromCgroupV2

func FromCgroupV2() (uint64, error)

FromCgroupV2 returns the memory limit from the cgroup v2.

func Limit

func Limit(limit uint64) func() (uint64, error)

Limit is a helper Provider function that returns the given limit.

func SetGoMemLimit

func SetGoMemLimit(ratio float64) (int64, error)

SetGoMemLimit sets GOMEMLIMIT with the value from the cgroup's memory limit and given ratio.

func SetGoMemLimitWithEnv added in v0.2.0

func SetGoMemLimitWithEnv()

SetGoMemLimitWithEnv sets GOMEMLIMIT with the value from the environment variable. You can configure how much memory of the cgroup's memory limit to set as GOMEMLIMIT through AUTOMEMLIMIT in the half-open range (0.0,1.0].

If AUTOMEMLIMIT is not set, it defaults to 0.9. (10% is the headroom for memory sources the Go runtime is unaware of.) If GOMEMLIMIT is already set or AUTOMEMLIMIT=off, this function does nothing.

func SetGoMemLimitWithOpts added in v0.3.0

func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error)

SetGoMemLimitWithOpts sets GOMEMLIMIT with options.

Options:

  • WithRatio
  • WithEnv (see more SetGoMemLimitWithEnv)
  • WithProvider

func SetGoMemLimitWithProvider

func SetGoMemLimitWithProvider(provider Provider, ratio float64) (int64, error)

SetGoMemLimitWithProvider sets GOMEMLIMIT with the value from the given provider and ratio.

Types

type Option added in v0.3.0

type Option func(cfg *config)

Option is a function that configures the behavior of SetGoMemLimitWithOptions.

func WithEnv added in v0.3.0

func WithEnv() Option

WithEnv configures whether to use environment variables.

Default: false

func WithProvider added in v0.3.0

func WithProvider(provider Provider) Option

WithProvider configures the provider.

Default: FromCgroup

func WithRatio added in v0.3.0

func WithRatio(ratio float64) Option

WithRatio configures the ratio of the memory limit to set as GOMEMLIMIT.

Default: 0.9

type Provider

type Provider func() (uint64, error)

Provider is a function that returns the memory limit.

Jump to

Keyboard shortcuts

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