From 0db9e34b69434baba9e24e41b2a3992cc191b418 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 22 Apr 2024 08:59:30 +0000 Subject: [PATCH] [chore]: Bump github.com/KimMachineGun/automemlimit from 0.5.0 to 0.6.0 (#2859) --- go.mod | 2 +- go.sum | 4 +- .../KimMachineGun/automemlimit/README.md | 5 +- .../automemlimit/automemlimit.go | 6 +- .../automemlimit/memlimit/logger.go | 13 +++ .../automemlimit/memlimit/memlimit.go | 84 +++++++++++++------ vendor/modules.txt | 4 +- 7 files changed, 86 insertions(+), 32 deletions(-) create mode 100644 vendor/github.com/KimMachineGun/automemlimit/memlimit/logger.go diff --git a/go.mod b/go.mod index 3d7c85fa4..3a89f13d0 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,7 @@ require ( codeberg.org/gruf/go-structr v0.6.2 codeberg.org/superseriousbusiness/exif-terminator v0.7.0 github.com/DmitriyVTitov/size v1.5.0 - github.com/KimMachineGun/automemlimit v0.5.0 + github.com/KimMachineGun/automemlimit v0.6.0 github.com/abema/go-mp4 v1.2.0 github.com/buckket/go-blurhash v1.1.0 github.com/coreos/go-oidc/v3 v3.10.0 diff --git a/go.sum b/go.sum index 046c507e8..07d0c2b5c 100644 --- a/go.sum +++ b/go.sum @@ -81,8 +81,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DmitriyVTitov/size v1.5.0 h1:/PzqxYrOyOUX1BXj6J9OuVRVGe+66VL4D9FlUaW515g= github.com/DmitriyVTitov/size v1.5.0/go.mod h1:le6rNI4CoLQV1b9gzp1+3d7hMAD/uu2QcJ+aYbNgiU0= -github.com/KimMachineGun/automemlimit v0.5.0 h1:BeOe+BbJc8L5chL3OwzVYjVzyvPALdd5wxVVOWuUZmQ= -github.com/KimMachineGun/automemlimit v0.5.0/go.mod h1:di3GCKiu9Y+1fs92erCbUvKzPkNyViN3mA0vti/ykEQ= +github.com/KimMachineGun/automemlimit v0.6.0 h1:p/BXkH+K40Hax+PuWWPQ478hPjsp9h1CPDhLlA3Z37E= +github.com/KimMachineGun/automemlimit v0.6.0/go.mod h1:T7xYht7B8r6AG/AqFcUdc7fzd2bIdBKmepfP2S1svPY= github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g= diff --git a/vendor/github.com/KimMachineGun/automemlimit/README.md b/vendor/github.com/KimMachineGun/automemlimit/README.md index c0e66d2a7..fa6c628f1 100644 --- a/vendor/github.com/KimMachineGun/automemlimit/README.md +++ b/vendor/github.com/KimMachineGun/automemlimit/README.md @@ -28,7 +28,8 @@ go get github.com/KimMachineGun/automemlimit@latest package main // By default, it sets `GOMEMLIMIT` to 90% of cgroup's memory limit. -// You can find more details of its behavior from the doc comment of memlimit.SetGoMemLimitWithEnv. +// This is equivalent to `memlimit.SetGoMemLimitWithOpts(memlimit.WithLogger(slog.Default()))` +// To disable logging, use `memlimit.SetGoMemLimitWithOpts` directly. import _ "github.com/KimMachineGun/automemlimit" ``` @@ -43,6 +44,7 @@ func init() { memlimit.SetGoMemLimitWithOpts( memlimit.WithRatio(0.9), memlimit.WithProvider(memlimit.FromCgroup), + memlimit.WithLogger(slog.Default()), ) memlimit.SetGoMemLimitWithOpts( memlimit.WithRatio(0.9), @@ -53,7 +55,6 @@ func init() { ), ), ) - memlimit.SetGoMemLimitWithEnv() memlimit.SetGoMemLimit(0.9) memlimit.SetGoMemLimitWithProvider(memlimit.Limit(1024*1024), 0.9) memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroup, 0.9) diff --git a/vendor/github.com/KimMachineGun/automemlimit/automemlimit.go b/vendor/github.com/KimMachineGun/automemlimit/automemlimit.go index 0480208ff..878312d60 100644 --- a/vendor/github.com/KimMachineGun/automemlimit/automemlimit.go +++ b/vendor/github.com/KimMachineGun/automemlimit/automemlimit.go @@ -1,9 +1,13 @@ package automemlimit import ( + "log/slog" + "github.com/KimMachineGun/automemlimit/memlimit" ) func init() { - memlimit.SetGoMemLimitWithEnv() + memlimit.SetGoMemLimitWithOpts( + memlimit.WithLogger(slog.Default()), + ) } diff --git a/vendor/github.com/KimMachineGun/automemlimit/memlimit/logger.go b/vendor/github.com/KimMachineGun/automemlimit/memlimit/logger.go new file mode 100644 index 000000000..4cf0b589d --- /dev/null +++ b/vendor/github.com/KimMachineGun/automemlimit/memlimit/logger.go @@ -0,0 +1,13 @@ +package memlimit + +import ( + "context" + "log/slog" +) + +type noopLogger struct{} + +func (noopLogger) Enabled(context.Context, slog.Level) bool { return false } +func (noopLogger) Handle(context.Context, slog.Record) error { return nil } +func (d noopLogger) WithAttrs([]slog.Attr) slog.Handler { return d } +func (d noopLogger) WithGroup(string) slog.Handler { return d } diff --git a/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go b/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go index 0d7a9853c..f7aaf071c 100644 --- a/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go +++ b/vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go @@ -3,8 +3,7 @@ package memlimit import ( "errors" "fmt" - "io" - "log" + "log/slog" "math" "os" "runtime/debug" @@ -12,8 +11,9 @@ import ( ) const ( - envGOMEMLIMIT = "GOMEMLIMIT" - envAUTOMEMLIMIT = "AUTOMEMLIMIT" + envGOMEMLIMIT = "GOMEMLIMIT" + envAUTOMEMLIMIT = "AUTOMEMLIMIT" + // Deprecated: use memlimit.WithLogger instead envAUTOMEMLIMIT_DEBUG = "AUTOMEMLIMIT_DEBUG" defaultAUTOMEMLIMIT = 0.9 @@ -25,7 +25,7 @@ var ( ) type config struct { - logger *log.Logger + logger *slog.Logger ratio float64 provider Provider } @@ -42,6 +42,25 @@ func WithRatio(ratio float64) Option { } } +// WithProvider configures the provider. +// +// Default: FromCgroup +func WithProvider(provider Provider) Option { + return func(cfg *config) { + cfg.provider = provider + } +} + +// WithLogger configures the logger. +// It automatically attaches the "package" attribute to the logs. +// +// Default: slog.New(noopLogger{}) +func WithLogger(logger *slog.Logger) Option { + return func(cfg *config) { + cfg.logger = memlimitLogger(logger) + } +} + // WithEnv configures whether to use environment variables. // // Default: false @@ -51,13 +70,11 @@ func WithEnv() Option { return func(cfg *config) {} } -// WithProvider configures the provider. -// -// Default: FromCgroup -func WithProvider(provider Provider) Option { - return func(cfg *config) { - cfg.provider = provider +func memlimitLogger(logger *slog.Logger) *slog.Logger { + if logger == nil { + return slog.New(noopLogger{}) } + return logger.With(slog.String("package", "github.com/KimMachineGun/automemlimit/memlimit")) } // SetGoMemLimitWithOpts sets GOMEMLIMIT with options and environment variables. @@ -74,54 +91,69 @@ func WithProvider(provider Provider) Option { // Options: // - WithRatio // - WithProvider +// - WithLogger func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { + // init config cfg := &config{ - logger: log.New(io.Discard, "", log.LstdFlags), + logger: slog.New(noopLogger{}), ratio: defaultAUTOMEMLIMIT, provider: FromCgroup, } - if os.Getenv(envAUTOMEMLIMIT_DEBUG) == "true" { - cfg.logger = log.Default() + // TODO: remove this + if debug, ok := os.LookupEnv(envAUTOMEMLIMIT_DEBUG); ok { + defaultLogger := memlimitLogger(slog.Default()) + defaultLogger.Warn("AUTOMEMLIMIT_DEBUG is deprecated, use memlimit.WithLogger instead") + if debug == "true" { + cfg.logger = defaultLogger + } } for _, opt := range opts { opt(cfg) } + + // log error if any on return defer func() { if _err != nil { - cfg.logger.Println(_err) + cfg.logger.Error("failed to set GOMEMLIMIT", slog.Any("error", _err)) } }() + // parse experiments exps, err := parseExperiments() if err != nil { return 0, fmt.Errorf("failed to parse experiments: %w", err) } if exps.System { - cfg.logger.Println("system experiment is enabled: using system memory limit as a fallback") + cfg.logger.Info("system experiment is enabled: using system memory limit as a fallback") cfg.provider = ApplyFallback(cfg.provider, FromSystem) } + // capture the current GOMEMLIMIT for rollback in case of panic snapshot := debug.SetMemoryLimit(-1) defer func() { - err := recover() - if err != nil { + panicErr := recover() + if panicErr != nil { if _err != nil { - cfg.logger.Println(_err) + cfg.logger.Error("failed to set GOMEMLIMIT", slog.Any("error", _err)) } - _err = fmt.Errorf("panic during setting the Go's memory limit, rolling back to previous value %d: %v", snapshot, err) + _err = fmt.Errorf("panic during setting the Go's memory limit, rolling back to previous limit %d: %v", + snapshot, panicErr, + ) debug.SetMemoryLimit(snapshot) } }() + // check if GOMEMLIMIT is already set if val, ok := os.LookupEnv(envGOMEMLIMIT); ok { - cfg.logger.Printf("GOMEMLIMIT is set already, skipping: %s\n", val) + cfg.logger.Info("GOMEMLIMIT is already set, skipping", slog.String(envGOMEMLIMIT, val)) return 0, nil } + // parse AUTOMEMLIMIT ratio := cfg.ratio if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok { if val == "off" { - cfg.logger.Printf("AUTOMEMLIMIT is set to off, skipping\n") + cfg.logger.Info("AUTOMEMLIMIT is set to off, skipping") return 0, nil } _ratio, err := strconv.ParseFloat(val, 64) @@ -131,20 +163,24 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { ratio = _ratio } + // set GOMEMLIMIT limit, err := setGoMemLimit(ApplyRatio(cfg.provider, ratio)) if err != nil { if errors.Is(err, ErrNoLimit) { - cfg.logger.Printf("memory is not limited, skipping: %v\n", err) + cfg.logger.Info("memory is not limited, skipping: %v", err) return 0, nil } return 0, fmt.Errorf("failed to set GOMEMLIMIT: %w", err) } - cfg.logger.Printf("GOMEMLIMIT=%d\n", limit) + cfg.logger.Info("GOMEMLIMIT is updated", slog.Int64(envGOMEMLIMIT, limit)) return limit, nil } +// SetGoMemLimitWithEnv sets GOMEMLIMIT with the value from the environment variables. +// Since WithEnv is deprecated, this function is equivalent to SetGoMemLimitWithOpts(). +// Deprecated: use SetGoMemLimitWithOpts instead. func SetGoMemLimitWithEnv() { _, _ = SetGoMemLimitWithOpts() } diff --git a/vendor/modules.txt b/vendor/modules.txt index a9a5d129f..d93f105d5 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -68,8 +68,8 @@ codeberg.org/superseriousbusiness/exif-terminator # github.com/DmitriyVTitov/size v1.5.0 ## explicit; go 1.14 github.com/DmitriyVTitov/size -# github.com/KimMachineGun/automemlimit v0.5.0 -## explicit; go 1.19 +# github.com/KimMachineGun/automemlimit v0.6.0 +## explicit; go 1.21 github.com/KimMachineGun/automemlimit github.com/KimMachineGun/automemlimit/memlimit # github.com/Masterminds/goutils v1.1.1