Bump github.com/KimMachineGun/automemlimit from 0.2.6 to 0.3.0

Bumps [github.com/KimMachineGun/automemlimit](https://github.com/KimMachineGun/automemlimit) from 0.2.6 to 0.3.0.
- [Commits](https://github.com/KimMachineGun/automemlimit/compare/v0.2.6...v0.3.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]
2023-09-04 12:59:18 +00:00
committed by Ralf Haferkamp
parent b627e7a0d6
commit 8d7389960c
5 changed files with 116 additions and 49 deletions

2
go.mod
View File

@@ -4,7 +4,7 @@ go 1.20
require (
github.com/CiscoM31/godata v1.0.8
github.com/KimMachineGun/automemlimit v0.2.6
github.com/KimMachineGun/automemlimit v0.3.0
github.com/Masterminds/semver v1.5.0
github.com/MicahParks/keyfunc v1.5.1
github.com/Nerzal/gocloak/v13 v13.8.0

4
go.sum
View File

@@ -643,8 +643,8 @@ github.com/CiscoM31/godata v1.0.8 h1:ZhPjm1dSwZWMUvb33P4bcVm048iiQ1wbncoCc9bLChQ
github.com/CiscoM31/godata v1.0.8/go.mod h1:ZMiT6JuD3Rm83HEtiTx4JEChsd25YCrxchKGag/sdTc=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk=
github.com/KimMachineGun/automemlimit v0.2.6 h1:tQFriVTcIteUkV5EgU9iz03eDY36T8JU5RAjP2r6Kt0=
github.com/KimMachineGun/automemlimit v0.2.6/go.mod h1:pJhTW/nWJMj6SnWSU2TEKSlCaM+1N5Mej+IfS/5/Ol0=
github.com/KimMachineGun/automemlimit v0.3.0 h1:khgwM5ESVN85cE6Bq2ozMAAWDfrOEwQ51D/YlmThE04=
github.com/KimMachineGun/automemlimit v0.3.0/go.mod h1:pJhTW/nWJMj6SnWSU2TEKSlCaM+1N5Mej+IfS/5/Ol0=
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 v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=

View File

@@ -32,6 +32,11 @@ package main
import "github.com/KimMachineGun/automemlimit/memlimit"
func init() {
memlimit.SetGoMemLimitWithOpts(
memlimit.WithRatio(0.9),
memlimit.WithEnv(),
memlimit.WithProvider(memlimit.FromCgroup),
)
memlimit.SetGoMemLimitWithEnv()
memlimit.SetGoMemLimit(0.9)
memlimit.SetGoMemLimitWithProvider(memlimit.Limit(1024*1024), 0.9)

View File

@@ -2,6 +2,7 @@ package memlimit
import (
"errors"
"fmt"
"io"
"log"
"math"
@@ -25,10 +26,113 @@ 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)
)
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.
//
// 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 {
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 val, ok := os.LookupEnv(envGOMEMLIMIT); ok {
cfg.logger.Printf("GOMEMLIMIT is set already, skipping: %s\n", val)
return
}
ratio := cfg.ratio
if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok {
if val == "off" {
cfg.logger.Printf("AUTOMEMLIMIT is set to off, skipping\n")
return
}
_ratio, err := strconv.ParseFloat(val, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse AUTOMEMLIMIT: %s", val)
}
ratio = _ratio
}
if ratio <= 0 || ratio > 1 {
return 0, fmt.Errorf("invalid AUTOMEMLIMIT: %f", ratio)
}
limit, err := SetGoMemLimitWithProvider(cfg.provider, ratio)
if err != nil {
return 0, fmt.Errorf("failed to set GOMEMLIMIT: %w", err)
}
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].
@@ -36,49 +140,7 @@ var (
// 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() {
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)
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)
return
}
ratio := defaultAUTOMEMLIMIT
if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok {
if val == "off" {
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
}
ratio = _ratio
}
if ratio <= 0 || ratio > 1 {
logger.Printf("invalid AUTOMEMLIMIT: %f\n", ratio)
return
}
limit, err := SetGoMemLimit(ratio)
if err != nil {
logger.Printf("failed to set GOMEMLIMIT: %v\n", err)
return
}
logger.Printf("GOMEMLIMIT=%d\n", limit)
_, _ = SetGoMemLimitWithOpts(WithEnv())
}
// SetGoMemLimit sets GOMEMLIMIT with the value from the cgroup's memory limit and given ratio.

2
vendor/modules.txt vendored
View File

@@ -11,7 +11,7 @@ github.com/BurntSushi/toml/internal
# github.com/CiscoM31/godata v1.0.8
## explicit; go 1.19
github.com/CiscoM31/godata
# github.com/KimMachineGun/automemlimit v0.2.6
# github.com/KimMachineGun/automemlimit v0.3.0
## explicit; go 1.19
github.com/KimMachineGun/automemlimit
github.com/KimMachineGun/automemlimit/memlimit