[chore]: Bump github.com/KimMachineGun/automemlimit from 0.5.0 to 0.6.0

Bumps [github.com/KimMachineGun/automemlimit](https://github.com/KimMachineGun/automemlimit) from 0.5.0 to 0.6.0.
- [Release notes](https://github.com/KimMachineGun/automemlimit/releases)
- [Commits](https://github.com/KimMachineGun/automemlimit/compare/v0.5.0...v0.6.0)

---
updated-dependencies:
- dependency-name: github.com/KimMachineGun/automemlimit
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
dependabot[bot] 2024-04-22 06:38:33 +00:00 committed by GitHub
parent b7c629a18a
commit 0e3c71e62e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 86 additions and 32 deletions

2
go.mod
View file

@ -24,7 +24,7 @@ require (
codeberg.org/gruf/go-structr v0.6.2 codeberg.org/gruf/go-structr v0.6.2
codeberg.org/superseriousbusiness/exif-terminator v0.7.0 codeberg.org/superseriousbusiness/exif-terminator v0.7.0
github.com/DmitriyVTitov/size v1.5.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/abema/go-mp4 v1.2.0
github.com/buckket/go-blurhash v1.1.0 github.com/buckket/go-blurhash v1.1.0
github.com/coreos/go-oidc/v3 v3.10.0 github.com/coreos/go-oidc/v3 v3.10.0

4
go.sum
View file

@ -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/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 h1:/PzqxYrOyOUX1BXj6J9OuVRVGe+66VL4D9FlUaW515g=
github.com/DmitriyVTitov/size v1.5.0/go.mod h1:le6rNI4CoLQV1b9gzp1+3d7hMAD/uu2QcJ+aYbNgiU0= 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.6.0 h1:p/BXkH+K40Hax+PuWWPQ478hPjsp9h1CPDhLlA3Z37E=
github.com/KimMachineGun/automemlimit v0.5.0/go.mod h1:di3GCKiu9Y+1fs92erCbUvKzPkNyViN3mA0vti/ykEQ= 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 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g= github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=

View file

@ -28,7 +28,8 @@ go get github.com/KimMachineGun/automemlimit@latest
package main package main
// By default, it sets `GOMEMLIMIT` to 90% of cgroup's memory limit. // 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" import _ "github.com/KimMachineGun/automemlimit"
``` ```
@ -43,6 +44,7 @@ func init() {
memlimit.SetGoMemLimitWithOpts( memlimit.SetGoMemLimitWithOpts(
memlimit.WithRatio(0.9), memlimit.WithRatio(0.9),
memlimit.WithProvider(memlimit.FromCgroup), memlimit.WithProvider(memlimit.FromCgroup),
memlimit.WithLogger(slog.Default()),
) )
memlimit.SetGoMemLimitWithOpts( memlimit.SetGoMemLimitWithOpts(
memlimit.WithRatio(0.9), memlimit.WithRatio(0.9),
@ -53,7 +55,6 @@ func init() {
), ),
), ),
) )
memlimit.SetGoMemLimitWithEnv()
memlimit.SetGoMemLimit(0.9) memlimit.SetGoMemLimit(0.9)
memlimit.SetGoMemLimitWithProvider(memlimit.Limit(1024*1024), 0.9) memlimit.SetGoMemLimitWithProvider(memlimit.Limit(1024*1024), 0.9)
memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroup, 0.9) memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroup, 0.9)

View file

@ -1,9 +1,13 @@
package automemlimit package automemlimit
import ( import (
"log/slog"
"github.com/KimMachineGun/automemlimit/memlimit" "github.com/KimMachineGun/automemlimit/memlimit"
) )
func init() { func init() {
memlimit.SetGoMemLimitWithEnv() memlimit.SetGoMemLimitWithOpts(
memlimit.WithLogger(slog.Default()),
)
} }

View file

@ -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 }

View file

@ -3,8 +3,7 @@ package memlimit
import ( import (
"errors" "errors"
"fmt" "fmt"
"io" "log/slog"
"log"
"math" "math"
"os" "os"
"runtime/debug" "runtime/debug"
@ -14,6 +13,7 @@ import (
const ( const (
envGOMEMLIMIT = "GOMEMLIMIT" envGOMEMLIMIT = "GOMEMLIMIT"
envAUTOMEMLIMIT = "AUTOMEMLIMIT" envAUTOMEMLIMIT = "AUTOMEMLIMIT"
// Deprecated: use memlimit.WithLogger instead
envAUTOMEMLIMIT_DEBUG = "AUTOMEMLIMIT_DEBUG" envAUTOMEMLIMIT_DEBUG = "AUTOMEMLIMIT_DEBUG"
defaultAUTOMEMLIMIT = 0.9 defaultAUTOMEMLIMIT = 0.9
@ -25,7 +25,7 @@ var (
) )
type config struct { type config struct {
logger *log.Logger logger *slog.Logger
ratio float64 ratio float64
provider Provider 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. // WithEnv configures whether to use environment variables.
// //
// Default: false // Default: false
@ -51,13 +70,11 @@ func WithEnv() Option {
return func(cfg *config) {} return func(cfg *config) {}
} }
// WithProvider configures the provider. func memlimitLogger(logger *slog.Logger) *slog.Logger {
// if logger == nil {
// Default: FromCgroup return slog.New(noopLogger{})
func WithProvider(provider Provider) Option {
return func(cfg *config) {
cfg.provider = provider
} }
return logger.With(slog.String("package", "github.com/KimMachineGun/automemlimit/memlimit"))
} }
// SetGoMemLimitWithOpts sets GOMEMLIMIT with options and environment variables. // SetGoMemLimitWithOpts sets GOMEMLIMIT with options and environment variables.
@ -74,54 +91,69 @@ func WithProvider(provider Provider) Option {
// Options: // Options:
// - WithRatio // - WithRatio
// - WithProvider // - WithProvider
// - WithLogger
func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) {
// init config
cfg := &config{ cfg := &config{
logger: log.New(io.Discard, "", log.LstdFlags), logger: slog.New(noopLogger{}),
ratio: defaultAUTOMEMLIMIT, ratio: defaultAUTOMEMLIMIT,
provider: FromCgroup, provider: FromCgroup,
} }
if os.Getenv(envAUTOMEMLIMIT_DEBUG) == "true" { // TODO: remove this
cfg.logger = log.Default() 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 { for _, opt := range opts {
opt(cfg) opt(cfg)
} }
// log error if any on return
defer func() { defer func() {
if _err != nil { if _err != nil {
cfg.logger.Println(_err) cfg.logger.Error("failed to set GOMEMLIMIT", slog.Any("error", _err))
} }
}() }()
// parse experiments
exps, err := parseExperiments() exps, err := parseExperiments()
if err != nil { if err != nil {
return 0, fmt.Errorf("failed to parse experiments: %w", err) return 0, fmt.Errorf("failed to parse experiments: %w", err)
} }
if exps.System { 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) cfg.provider = ApplyFallback(cfg.provider, FromSystem)
} }
// capture the current GOMEMLIMIT for rollback in case of panic
snapshot := debug.SetMemoryLimit(-1) snapshot := debug.SetMemoryLimit(-1)
defer func() { defer func() {
err := recover() panicErr := recover()
if err != nil { if panicErr != nil {
if _err != 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) debug.SetMemoryLimit(snapshot)
} }
}() }()
// check if GOMEMLIMIT is already set
if val, ok := os.LookupEnv(envGOMEMLIMIT); ok { 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 return 0, nil
} }
// parse AUTOMEMLIMIT
ratio := cfg.ratio ratio := cfg.ratio
if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok { if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok {
if val == "off" { 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 return 0, nil
} }
_ratio, err := strconv.ParseFloat(val, 64) _ratio, err := strconv.ParseFloat(val, 64)
@ -131,20 +163,24 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) {
ratio = _ratio ratio = _ratio
} }
// set GOMEMLIMIT
limit, err := setGoMemLimit(ApplyRatio(cfg.provider, ratio)) limit, err := setGoMemLimit(ApplyRatio(cfg.provider, ratio))
if err != nil { if err != nil {
if errors.Is(err, ErrNoLimit) { 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, nil
} }
return 0, fmt.Errorf("failed to set GOMEMLIMIT: %w", err) 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 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() { func SetGoMemLimitWithEnv() {
_, _ = SetGoMemLimitWithOpts() _, _ = SetGoMemLimitWithOpts()
} }

4
vendor/modules.txt vendored
View file

@ -68,8 +68,8 @@ codeberg.org/superseriousbusiness/exif-terminator
# github.com/DmitriyVTitov/size v1.5.0 # github.com/DmitriyVTitov/size v1.5.0
## explicit; go 1.14 ## explicit; go 1.14
github.com/DmitriyVTitov/size github.com/DmitriyVTitov/size
# github.com/KimMachineGun/automemlimit v0.5.0 # github.com/KimMachineGun/automemlimit v0.6.0
## explicit; go 1.19 ## explicit; go 1.21
github.com/KimMachineGun/automemlimit github.com/KimMachineGun/automemlimit
github.com/KimMachineGun/automemlimit/memlimit github.com/KimMachineGun/automemlimit/memlimit
# github.com/Masterminds/goutils v1.1.1 # github.com/Masterminds/goutils v1.1.1