diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a51fc17..b573fcd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,15 +15,15 @@ jobs: - name: Pull golang image run: | - docker pull golang:1.21 + docker pull golang:1.19 - name: Run tests in Go container (1000m) run: | - docker run --rm -v=$(pwd):/app -w=/app -m=1000m golang:1.21 go test -v ./... -expected=1048576000 + docker run --rm -v=$(pwd):/app -w=/app -m=1000m golang:1.19 go test -v ./... -expected=1048576000 - name: Run tests in Go container (4321m) run: | - docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.21 go test -v ./... -expected=4530896896 + docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.19 go test -v ./... -expected=4530896896 test-ubuntu-22_04: runs-on: ubuntu-22.04 @@ -37,12 +37,12 @@ jobs: - name: Pull golang image run: | - docker pull golang:1.21 + docker pull golang:1.19 - name: Run tests in Go container (1000m) run: | - docker run --rm -v=$(pwd):/app -w=/app -m=1000m golang:1.21 go test -v ./... -expected=1048576000 + docker run --rm -v=$(pwd):/app -w=/app -m=1000m golang:1.19 go test -v ./... -expected=1048576000 - name: Run tests in Go container (4321m) run: | - docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.21 go test -v ./... -expected=4530896896 + docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.19 go test -v ./... -expected=4530896896 diff --git a/README.md b/README.md index 2df8c60..4f60050 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ go get github.com/KimMachineGun/automemlimit@latest package main // By default, it sets `GOMEMLIMIT` to 90% of cgroup's memory limit. -// This is equivalent to `memlimit.SetGoMemLimitWithOpts(memlimit.WithEnv(), memlimit.WithLogger(slog.Default()))`` +// You can find more details of its behavior from the doc comment of memlimit.SetGoMemLimitWithEnv. import _ "github.com/KimMachineGun/automemlimit" ``` @@ -35,7 +35,6 @@ func init() { memlimit.SetGoMemLimitWithOpts( memlimit.WithRatio(0.9), memlimit.WithProvider(memlimit.FromCgroup), - memlimit.WithLogger(slog.Default()), ) memlimit.SetGoMemLimitWithEnv() memlimit.SetGoMemLimit(0.9) diff --git a/automemlimit.go b/automemlimit.go index 878312d..0480208 100644 --- a/automemlimit.go +++ b/automemlimit.go @@ -1,13 +1,9 @@ package automemlimit import ( - "log/slog" - "github.com/KimMachineGun/automemlimit/memlimit" ) func init() { - memlimit.SetGoMemLimitWithOpts( - memlimit.WithLogger(slog.Default()), - ) + memlimit.SetGoMemLimitWithEnv() } diff --git a/examples/gosigar/go.mod b/examples/gosigar/go.mod index 265ed5e..a5660f1 100644 --- a/examples/gosigar/go.mod +++ b/examples/gosigar/go.mod @@ -1,6 +1,8 @@ module github.com/KimMachineGun/automemlimit/examples/gosigar -go 1.21.0 +go 1.21 + +toolchain go1.21.0 require ( github.com/KimMachineGun/automemlimit v0.0.0 diff --git a/examples/logger/go.mod b/examples/logger/go.mod deleted file mode 100644 index 2e2f98d..0000000 --- a/examples/logger/go.mod +++ /dev/null @@ -1,20 +0,0 @@ -module github.com/KimMachineGun/automemlimit/examples/logger - -go 1.21.0 - -require github.com/KimMachineGun/automemlimit v0.0.0 - -require ( - github.com/cilium/ebpf v0.9.1 // indirect - github.com/containerd/cgroups/v3 v3.0.1 // indirect - github.com/coreos/go-systemd/v22 v22.3.2 // indirect - github.com/docker/go-units v0.4.0 // indirect - github.com/godbus/dbus/v5 v5.0.4 // indirect - github.com/google/go-cmp v0.5.9 // indirect - github.com/opencontainers/runtime-spec v1.0.2 // indirect - github.com/sirupsen/logrus v1.8.1 // indirect - golang.org/x/sys v0.13.0 // indirect - google.golang.org/protobuf v1.27.1 // indirect -) - -replace github.com/KimMachineGun/automemlimit => ../../ diff --git a/examples/logger/go.sum b/examples/logger/go.sum deleted file mode 100644 index 039609f..0000000 --- a/examples/logger/go.sum +++ /dev/null @@ -1,44 +0,0 @@ -github.com/cilium/ebpf v0.9.1 h1:64sn2K3UKw8NbP/blsixRpF3nXuyhz/VjRlRzvlBRu4= -github.com/cilium/ebpf v0.9.1/go.mod h1:+OhNOIXx/Fnu1IE8bJz2dzOA+VSfyTfdNUVdlQnxUFY= -github.com/containerd/cgroups/v3 v3.0.1 h1:4hfGvu8rfGIwVIDd+nLzn/B9ZXx4BcCjzt5ToenJRaE= -github.com/containerd/cgroups/v3 v3.0.1/go.mod h1:/vtwk1VXrtoa5AaZLkypuOJgA/6DyPMZHJPGQNtlHnw= -github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI= -github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= -github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= -github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss= -github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og= -github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA= -github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= -github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/opencontainers/runtime-spec v1.0.2 h1:UfAcuLBJB9Coz72x1hgl8O5RVzTdNiaglX6v2DM6FI0= -github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= -github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= -github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= -github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= -go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= -golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= -google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/logger/main.go b/examples/logger/main.go deleted file mode 100644 index 3149710..0000000 --- a/examples/logger/main.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import ( - "log/slog" - "os" - - "github.com/KimMachineGun/automemlimit/memlimit" -) - -func init() { - memlimit.SetGoMemLimitWithOpts( - memlimit.WithProvider( - memlimit.Limit(1024*1024*1024), - ), - memlimit.WithLogger(slog.New(slog.NewJSONHandler(os.Stderr, nil))), - ) -} - -func main() {} diff --git a/go.mod b/go.mod index d78c2aa..c38ce4c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/KimMachineGun/automemlimit -go 1.21.0 +go 1.19 require ( github.com/containerd/cgroups/v3 v3.0.3 diff --git a/go.sum b/go.sum index f13c8be..b4a28c4 100644 --- a/go.sum +++ b/go.sum @@ -10,17 +10,13 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA= -github.com/frankban/quicktest v1.14.5/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/opencontainers/runtime-spec v1.0.2 h1:UfAcuLBJB9Coz72x1hgl8O5RVzTdNiaglX6v2DM6FI0= github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0= @@ -28,15 +24,12 @@ github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhM github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= -github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= -go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 h1:Jvc7gsqn21cJHCmAWx0LiimpP18LZmUxkT5Mp7EZ1mI= golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -49,4 +42,3 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/memlimit/logger.go b/memlimit/logger.go deleted file mode 100644 index 4cf0b58..0000000 --- a/memlimit/logger.go +++ /dev/null @@ -1,13 +0,0 @@ -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/memlimit/memlimit.go b/memlimit/memlimit.go index 9d96f08..525f19f 100644 --- a/memlimit/memlimit.go +++ b/memlimit/memlimit.go @@ -3,7 +3,8 @@ package memlimit import ( "errors" "fmt" - "log/slog" + "io" + "log" "math" "os" "runtime/debug" @@ -11,9 +12,8 @@ import ( ) const ( - envGOMEMLIMIT = "GOMEMLIMIT" - envAUTOMEMLIMIT = "AUTOMEMLIMIT" - // Deprecated: use memlimit.WithLogger instead + envGOMEMLIMIT = "GOMEMLIMIT" + envAUTOMEMLIMIT = "AUTOMEMLIMIT" envAUTOMEMLIMIT_DEBUG = "AUTOMEMLIMIT_DEBUG" defaultAUTOMEMLIMIT = 0.9 @@ -25,7 +25,7 @@ var ( ) type config struct { - logger *slog.Logger + logger *log.Logger ratio float64 provider Provider } @@ -60,23 +60,6 @@ func WithProvider(provider Provider) Option { } } -// 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) - } -} - -func memlimitLogger(logger *slog.Logger) *slog.Logger { - if logger == nil { - return slog.New(noopLogger{}) - } - return logger.With(slog.String("package", "memlimit")) -} - // SetGoMemLimitWithOpts sets GOMEMLIMIT with options and environment variables. // // You can configure how much memory of the cgroup's memory limit to set as GOMEMLIMIT @@ -91,27 +74,21 @@ func memlimitLogger(logger *slog.Logger) *slog.Logger { // Options: // - WithRatio // - WithProvider -// - WithLogger func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { cfg := &config{ - logger: slog.New(noopLogger{}), + logger: log.New(io.Discard, "", log.LstdFlags), ratio: defaultAUTOMEMLIMIT, provider: FromCgroup, } - // TODO: remove this - if debug, ok := os.LookupEnv(envAUTOMEMLIMIT_DEBUG); ok { - logger := memlimitLogger(slog.Default()) - logger.Warn("AUTOMEMLIMIT_DEBUG is deprecated, use memlimit.WithLogger instead") - if debug == "true" { - cfg.logger = logger - } + if os.Getenv(envAUTOMEMLIMIT_DEBUG) == "true" { + cfg.logger = log.Default() } for _, opt := range opts { opt(cfg) } defer func() { if _err != nil { - cfg.logger.Error("failed to set GOMEMLIMIT", slog.Any("error", _err)) + cfg.logger.Println(_err) } }() @@ -128,7 +105,7 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { err := recover() if err != nil { if _err != nil { - cfg.logger.Error("failed to set GOMEMLIMIT", slog.Any("error", _err)) + 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) @@ -136,14 +113,14 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { }() if val, ok := os.LookupEnv(envGOMEMLIMIT); ok { - cfg.logger.Info("GOMEMLIMIT is set already, skipping", slog.String("GOMEMLIMIT", val)) + cfg.logger.Printf("GOMEMLIMIT is set already, skipping: %s\n", val) return 0, nil } ratio := cfg.ratio if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok { if val == "off" { - cfg.logger.Debug("AUTOMEMLIMIT is set to off, skipping") + cfg.logger.Printf("AUTOMEMLIMIT is set to off, skipping\n") return 0, nil } _ratio, err := strconv.ParseFloat(val, 64) @@ -158,7 +135,7 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { return 0, fmt.Errorf("failed to set GOMEMLIMIT: %w", err) } - cfg.logger.Info("GOMEMLIMIT is set", slog.Int64("GOMEMLIMIT", limit)) + cfg.logger.Printf("GOMEMLIMIT=%d\n", limit) return limit, nil }