summaryrefslogtreecommitdiff
path: root/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
diff options
context:
space:
mode:
authorLibravatar dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>2023-08-28 06:59:08 +0000
committerLibravatar GitHub <noreply@github.com>2023-08-28 06:59:08 +0000
commite6407ec95c1c0b134866d7bd4b2d17f4b0c3bf39 (patch)
tree4085dc4a80680a2f480e0466a984e3097f4c58b4 /vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
parent[chore/frontend] Make line-height a wee little bit bigger (#2159) (diff)
downloadgotosocial-e6407ec95c1c0b134866d7bd4b2d17f4b0c3bf39.tar.xz
[chore]: Bump github.com/KimMachineGun/automemlimit from 0.2.6 to 0.3.0 (#2165)
Diffstat (limited to 'vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go')
-rw-r--r--vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go110
1 files changed, 86 insertions, 24 deletions
diff --git a/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go b/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
index d2492ae9f..4ab0fc6f8 100644
--- a/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
+++ b/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
@@ -2,6 +2,7 @@ package memlimit
import (
"errors"
+ "fmt"
"io"
"log"
"math"
@@ -25,60 +26,121 @@ var (
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")
-
- logger = log.New(io.Discard, "", log.LstdFlags)
)
-// 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].
+type config struct {
+ logger *log.Logger
+ ratio float64
+ env bool
+ provider Provider
+}
+
+// Option is a function that configures the behavior of SetGoMemLimitWithOptions.
+type Option func(cfg *config)
+
+// WithRatio configures the ratio of the memory limit to set as GOMEMLIMIT.
//
-// 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 SetGoMemLimitWithEnv() {
+// Default: 0.9
+func WithRatio(ratio float64) Option {
+ return func(cfg *config) {
+ cfg.ratio = ratio
+ }
+}
+
+// WithEnv configures whether to use environment variables.
+//
+// Default: false
+func WithEnv() Option {
+ return func(cfg *config) {
+ cfg.env = true
+ }
+}
+
+// WithProvider configures the provider.
+//
+// Default: FromCgroup
+func WithProvider(provider Provider) Option {
+ return func(cfg *config) {
+ cfg.provider = provider
+ }
+}
+
+// SetGoMemLimitWithOpts sets GOMEMLIMIT with options.
+//
+// Options:
+// - WithRatio
+// - WithEnv (see more SetGoMemLimitWithEnv)
+// - WithProvider
+func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) {
+ cfg := &config{
+ logger: log.New(io.Discard, "", log.LstdFlags),
+ ratio: defaultAUTOMEMLIMIT,
+ env: false,
+ provider: FromCgroup,
+ }
+ if os.Getenv(envAUTOMEMLIMIT_DEBUG) == "true" {
+ cfg.logger = log.Default()
+ }
+ for _, opt := range opts {
+ opt(cfg)
+ }
+ defer func() {
+ if _err != nil {
+ cfg.logger.Println(_err)
+ }
+ }()
+
snapshot := debug.SetMemoryLimit(-1)
defer func() {
err := recover()
if err != nil {
- logger.Printf("panic during SetGoMemLimitWithEnv, rolling back to previous value %d: %v\n", snapshot, err)
+ if _err != nil {
+ cfg.logger.Println(_err)
+ }
+ _err = fmt.Errorf("panic during setting the Go's memory limit, rolling back to previous value %d: %v", snapshot, err)
debug.SetMemoryLimit(snapshot)
}
}()
- if os.Getenv(envAUTOMEMLIMIT_DEBUG) == "true" {
- logger = log.Default()
- }
-
if val, ok := os.LookupEnv(envGOMEMLIMIT); ok {
- logger.Printf("GOMEMLIMIT is set already, skipping: %s\n", val)
+ cfg.logger.Printf("GOMEMLIMIT is set already, skipping: %s\n", val)
return
}
- ratio := defaultAUTOMEMLIMIT
+ ratio := cfg.ratio
if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok {
if val == "off" {
- logger.Printf("AUTOMEMLIMIT is set to off, skipping\n")
+ cfg.logger.Printf("AUTOMEMLIMIT is set to off, skipping\n")
return
}
_ratio, err := strconv.ParseFloat(val, 64)
if err != nil {
- logger.Printf("cannot parse AUTOMEMLIMIT: %s\n", val)
- return
+ return 0, fmt.Errorf("cannot parse AUTOMEMLIMIT: %s", val)
}
ratio = _ratio
}
if ratio <= 0 || ratio > 1 {
- logger.Printf("invalid AUTOMEMLIMIT: %f\n", ratio)
- return
+ return 0, fmt.Errorf("invalid AUTOMEMLIMIT: %f", ratio)
}
- limit, err := SetGoMemLimit(ratio)
+ limit, err := SetGoMemLimitWithProvider(cfg.provider, ratio)
if err != nil {
- logger.Printf("failed to set GOMEMLIMIT: %v\n", err)
- return
+ return 0, fmt.Errorf("failed to set GOMEMLIMIT: %w", err)
}
- logger.Printf("GOMEMLIMIT=%d\n", limit)
+ cfg.logger.Printf("GOMEMLIMIT=%d\n", limit)
+
+ return limit, nil
+}
+
+// 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 SetGoMemLimitWithEnv() {
+ _, _ = SetGoMemLimitWithOpts(WithEnv())
}
// SetGoMemLimit sets GOMEMLIMIT with the value from the cgroup's memory limit and given ratio.