From a537451ab1aa043285e52ee413f31d5286303dc4 Mon Sep 17 00:00:00 2001 From: Dave Poole Date: Fri, 9 Feb 2024 11:06:03 -0800 Subject: [PATCH] clean up logic a bit --- main/vmWatch.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/main/vmWatch.go b/main/vmWatch.go index decad3f..ca19a63 100644 --- a/main/vmWatch.go +++ b/main/vmWatch.go @@ -22,8 +22,8 @@ import ( type VMWatchStatus string const ( - MaxCpuQuota = 1 // 1% cpu - MaxMemoryInBytes = 80000000 // 80MB + DefaultMaxCpuPercentage = 1 // 1% cpu + DefaultMaxMemoryInBytes = 80000000 // 80MB HoursBetweenRetryAttempts = 3 CGroupV2PeriodMs = 1000000 // 1 second ) @@ -230,26 +230,25 @@ func setupVMWatchCommand(s *vmWatchSettings, hEnv HandlerEnvironment) (*exec.Cmd args = append(args, "--execution-environment", GetExecutionEnvironment(hEnv)) // 0 is the default so allow that but any value below 30MB is not allowed - if s.MemoryLimitInBytes != 0 && (s.MemoryLimitInBytes < 0 || s.MemoryLimitInBytes < 30000000) { + if s.MemoryLimitInBytes == 0 { + s.MemoryLimitInBytes = DefaultMaxMemoryInBytes + + } + if s.MemoryLimitInBytes < 30000000 { err = fmt.Errorf("[%v] Invalid MemoryLimitInBytes specified must be at least 30000000", time.Now().UTC().Format(time.RFC3339)) return nil, err } - // set the default value if not specified - if s.MemoryLimitInBytes == 0 { - s.MemoryLimitInBytes = MaxMemoryInBytes + // check cpu, if 0 (default) set to the default value + if (s.MaxCpuPercentage == 0) { + s.MaxCpuPercentage = DefaultMaxCpuPercentage } - // check if the maxCpuPercentage is valid if s.MaxCpuPercentage < 0 || s.MaxCpuPercentage > 100 { err = fmt.Errorf("[%v] Invalid maxCpuPercentage specified must be between 0 and 100", time.Now().UTC().Format(time.RFC3339)) return nil, err } - if s.MaxCpuPercentage == 0 { - s.MaxCpuPercentage = MaxCpuQuota - } - args = append(args, "--memory-limit-bytes", strconv.FormatInt(s.MemoryLimitInBytes, 10)) if s.SignalFilters != nil {