Commit c296d7e7 authored by detaoin's avatar detaoin Committed by Matt Holt

caddymain: fix setCPU silently ignoring small percent values (#1969)

* caddymain: fix setCPU silently ignoring small percent values

the percent value is resolved in a GOMAXPROCS relative number by simple
division, thus rounding down the non-integer quotient. If zero, the call
to runtime.GOMAXPROCS is silently ignored.

We decide here to exceptionally round up the CPU cap in case of percent
values that are too small.

* caddymain: gofmt -s
parent fc1509ee
...@@ -221,6 +221,8 @@ func setVersion() { ...@@ -221,6 +221,8 @@ func setVersion() {
// setCPU parses string cpu and sets GOMAXPROCS // setCPU parses string cpu and sets GOMAXPROCS
// according to its value. It accepts either // according to its value. It accepts either
// a number (e.g. 3) or a percent (e.g. 50%). // a number (e.g. 3) or a percent (e.g. 50%).
// If the percent resolves to less than a single
// GOMAXPROCS, it rounds it up to GOMAXPROCS=1.
func setCPU(cpu string) error { func setCPU(cpu string) error {
var numCPU int var numCPU int
...@@ -236,6 +238,9 @@ func setCPU(cpu string) error { ...@@ -236,6 +238,9 @@ func setCPU(cpu string) error {
} }
percent = float32(pctInt) / 100 percent = float32(pctInt) / 100
numCPU = int(float32(availCPU) * percent) numCPU = int(float32(availCPU) * percent)
if numCPU < 1 {
numCPU = 1
}
} else { } else {
// Number // Number
num, err := strconv.Atoi(cpu) num, err := strconv.Atoi(cpu)
......
...@@ -41,6 +41,7 @@ func TestSetCPU(t *testing.T) { ...@@ -41,6 +41,7 @@ func TestSetCPU(t *testing.T) {
{"invalid input", currentCPU, true}, {"invalid input", currentCPU, true},
{"invalid input%", currentCPU, true}, {"invalid input%", currentCPU, true},
{"9999", maxCPU, false}, // over available CPU {"9999", maxCPU, false}, // over available CPU
{"1%", 1, false}, // under a single CPU; assume maxCPU < 100
} { } {
err := setCPU(test.input) err := setCPU(test.input)
if test.shouldErr && err == nil { if test.shouldErr && err == nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment