Skip to content

Commit

Permalink
chore(memlimit): leave some comments for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
KimMachineGun committed Mar 9, 2024
1 parent 536043f commit 848c2e6
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions memlimit/memlimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,32 @@ func memlimitLogger(logger *slog.Logger) *slog.Logger {
// - WithProvider
// - WithLogger
func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) {
// init config
cfg := &config{
logger: slog.New(noopLogger{}),
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")
defaultLogger := memlimitLogger(slog.Default())
defaultLogger.Warn("AUTOMEMLIMIT_DEBUG is deprecated, use memlimit.WithLogger instead")
if debug == "true" {
cfg.logger = logger
cfg.logger = defaultLogger
}
}
for _, opt := range opts {
opt(cfg)
}

// log error if any on return
defer func() {
if _err != nil {
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)
Expand All @@ -124,23 +128,28 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) {
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.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.Info("GOMEMLIMIT is set already, skipping", slog.String("GOMEMLIMIT", val))
return 0, nil
}

// parse AUTOMEMLIMIT
ratio := cfg.ratio
if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok {
if val == "off" {
Expand All @@ -154,6 +163,7 @@ 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) {
Expand Down

0 comments on commit 848c2e6

Please sign in to comment.