summaryrefslogtreecommitdiff
path: root/vendor/github.com/KimMachineGun/automemlimit/memlimit
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/KimMachineGun/automemlimit/memlimit')
-rw-r--r--vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go7
-rw-r--r--vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups_unsupported.go2
-rw-r--r--vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go37
-rw-r--r--vendor/github.com/KimMachineGun/automemlimit/memlimit/provider.go40
4 files changed, 58 insertions, 28 deletions
diff --git a/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go b/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go
index a5c22de32..cfe6d0f60 100644
--- a/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go
+++ b/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups.go
@@ -21,7 +21,7 @@ func FromCgroup() (uint64, error) {
case cgroups.Legacy:
return FromCgroupV1()
case cgroups.Hybrid:
- return fromCgroupHybrid()
+ return FromCgroupHybrid()
case cgroups.Unified:
return FromCgroupV2()
}
@@ -49,10 +49,9 @@ func FromCgroupV1() (uint64, error) {
return 0, ErrNoLimit
}
-// fromCgroupHybrid returns the memory limit from the cgroup v1 or v2.
+// FromCgroupHybrid returns the memory limit from the cgroup v1 or v2.
// It checks the cgroup v2 first, and if it fails, it falls back to cgroup v1.
-// TODO: make this function public in the next minor version.
-func fromCgroupHybrid() (uint64, error) {
+func FromCgroupHybrid() (uint64, error) {
limit, err := fromCgroupV2(filepath.Join(cgroupMountPoint, "unified"))
if err == nil {
return limit, nil
diff --git a/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups_unsupported.go b/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups_unsupported.go
index 62f9bcfa4..9feca81a5 100644
--- a/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups_unsupported.go
+++ b/vendor/github.com/KimMachineGun/automemlimit/memlimit/cgroups_unsupported.go
@@ -11,7 +11,7 @@ func FromCgroupV1() (uint64, error) {
return 0, ErrCgroupsNotSupported
}
-func fromCgroupHybrid() (uint64, error) {
+func FromCgroupHybrid() (uint64, error) {
return 0, ErrCgroupsNotSupported
}
diff --git a/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go b/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
index 4ab0fc6f8..dbb4d1c72 100644
--- a/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
+++ b/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
@@ -104,14 +104,14 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) {
if val, ok := os.LookupEnv(envGOMEMLIMIT); ok {
cfg.logger.Printf("GOMEMLIMIT is set already, skipping: %s\n", val)
- return
+ return 0, nil
}
ratio := cfg.ratio
if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok {
if val == "off" {
cfg.logger.Printf("AUTOMEMLIMIT is set to off, skipping\n")
- return
+ return 0, nil
}
_ratio, err := strconv.ParseFloat(val, 64)
if err != nil {
@@ -119,11 +119,8 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) {
}
ratio = _ratio
}
- if ratio <= 0 || ratio > 1 {
- return 0, fmt.Errorf("invalid AUTOMEMLIMIT: %f", ratio)
- }
- limit, err := SetGoMemLimitWithProvider(cfg.provider, ratio)
+ limit, err := setGoMemLimit(ApplyRatio(cfg.provider, ratio))
if err != nil {
return 0, fmt.Errorf("failed to set GOMEMLIMIT: %w", err)
}
@@ -145,33 +142,27 @@ func SetGoMemLimitWithEnv() {
// SetGoMemLimit sets GOMEMLIMIT with the value from the cgroup's memory limit and given ratio.
func SetGoMemLimit(ratio float64) (int64, error) {
- return SetGoMemLimitWithProvider(FromCgroup, ratio)
+ return SetGoMemLimitWithOpts(WithRatio(ratio))
}
-// Provider is a function that returns the memory limit.
-type Provider func() (uint64, error)
-
// SetGoMemLimitWithProvider sets GOMEMLIMIT with the value from the given provider and ratio.
func SetGoMemLimitWithProvider(provider Provider, ratio float64) (int64, error) {
+ return SetGoMemLimitWithOpts(WithProvider(provider), WithRatio(ratio))
+}
+
+func setGoMemLimit(provider Provider) (int64, error) {
limit, err := provider()
if err != nil {
return 0, err
}
- goMemLimit := cappedFloat2Int(float64(limit) * ratio)
- debug.SetMemoryLimit(goMemLimit)
- return goMemLimit, nil
+ capped := cappedU64ToI64(limit)
+ debug.SetMemoryLimit(capped)
+ return capped, nil
}
-func cappedFloat2Int(f float64) int64 {
- if f > math.MaxInt64 {
+func cappedU64ToI64(limit uint64) int64 {
+ if limit > math.MaxInt64 {
return math.MaxInt64
}
- return int64(f)
-}
-
-// Limit is a helper Provider function that returns the given limit.
-func Limit(limit uint64) func() (uint64, error) {
- return func() (uint64, error) {
- return limit, nil
- }
+ return int64(limit)
}
diff --git a/vendor/github.com/KimMachineGun/automemlimit/memlimit/provider.go b/vendor/github.com/KimMachineGun/automemlimit/memlimit/provider.go
new file mode 100644
index 000000000..32cc1eea6
--- /dev/null
+++ b/vendor/github.com/KimMachineGun/automemlimit/memlimit/provider.go
@@ -0,0 +1,40 @@
+package memlimit
+
+import (
+ "fmt"
+)
+
+// Provider is a function that returns the memory limit.
+type Provider func() (uint64, error)
+
+// Limit is a helper Provider function that returns the given limit.
+func Limit(limit uint64) func() (uint64, error) {
+ return func() (uint64, error) {
+ return limit, nil
+ }
+}
+
+// ApplyRationA is a helper Provider function that applies the given ratio to the given provider.
+func ApplyRatio(provider Provider, ratio float64) Provider {
+ return func() (uint64, error) {
+ if ratio <= 0 || ratio > 1 {
+ return 0, fmt.Errorf("invalid ratio: %f, ratio should be in the range (0.0,1.0]", ratio)
+ }
+ limit, err := provider()
+ if err != nil {
+ return 0, err
+ }
+ return uint64(float64(limit) * ratio), nil
+ }
+}
+
+// ApplyFallback is a helper Provider function that sets the fallback provider.
+func ApplyFallback(provider Provider, fallback Provider) Provider {
+ return func() (uint64, error) {
+ limit, err := provider()
+ if err != nil {
+ return fallback()
+ }
+ return limit, nil
+ }
+}