diff --git a/caddy/caddymain/run.go b/caddy/caddymain/run.go index 81f97f2a..82087d8f 100644 --- a/caddy/caddymain/run.go +++ b/caddy/caddymain/run.go @@ -221,6 +221,8 @@ func setVersion() { // setCPU parses string cpu and sets GOMAXPROCS // according to its value. It accepts either // 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 { var numCPU int @@ -236,6 +238,9 @@ func setCPU(cpu string) error { } percent = float32(pctInt) / 100 numCPU = int(float32(availCPU) * percent) + if numCPU < 1 { + numCPU = 1 + } } else { // Number num, err := strconv.Atoi(cpu) diff --git a/caddy/caddymain/run_test.go b/caddy/caddymain/run_test.go index 141efe20..c26a54a9 100644 --- a/caddy/caddymain/run_test.go +++ b/caddy/caddymain/run_test.go @@ -41,6 +41,7 @@ func TestSetCPU(t *testing.T) { {"invalid input", currentCPU, true}, {"invalid input%", currentCPU, true}, {"9999", maxCPU, false}, // over available CPU + {"1%", 1, false}, // under a single CPU; assume maxCPU < 100 } { err := setCPU(test.input) if test.shouldErr && err == nil {