From c296d7e7e06c3fe5e4877c2b1b0df153917a2b24 Mon Sep 17 00:00:00 2001 From: detaoin Date: Tue, 16 Jan 2018 02:17:27 +0100 Subject: [PATCH] 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 --- caddy/caddymain/run.go | 5 +++++ caddy/caddymain/run_test.go | 1 + 2 files changed, 6 insertions(+) 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 {