Commit dd6a5f5a authored by Jeremy Morrell's avatar Jeremy Morrell Committed by GitHub

Pad WEB_CONCURRENCY with a leading zero (#389)

Prepend 0 to auto-computed WEB_CONCURRENCY values

When customers run Node together with PHP or Python, then Node's .profile.d/ script will set a WEB_CONCURRENCY variable depending on dyno size. The Python .profile.d script (due to alphabetical order) and the PHP "boot scripts" then see a WEB_CONCURRENCY value and use it, as they cannot know that the variable was set not by a customer using heroku config:set, but by another buildpack's dyno .profile.d/ script.

This change prepends a leading zero to the WEB_CONCURRENCY variable set by the Node buildpack, which the Python and PHP buildpacks can detect and choose to ignore, but should not affect customers using the variable.

JS has some weirdness with leading zeros being interpreted as octal, but that doesn't appear to affect us here.
parent db7274dc
calculate_concurrency() {
MEMORY_AVAILABLE=${MEMORY_AVAILABLE-$(detect_memory 512)}
WEB_MEMORY=${WEB_MEMORY-512}
WEB_CONCURRENCY=${WEB_CONCURRENCY-$((MEMORY_AVAILABLE/WEB_MEMORY))}
if (( WEB_CONCURRENCY < 1 )); then
WEB_CONCURRENCY=1
local memory_available=$1
local web_memory=$2
local concurrency=$((memory_available/web_memory))
if (( concurrency < 1 )); then
concurrency=1
fi
WEB_CONCURRENCY=$WEB_CONCURRENCY
# We prepend the calculated value with a leading '0' so that other buildpacks
# can distinguish between a value set by the Node buildpack and a value set
# by the user
echo "0$concurrency"
}
log_concurrency() {
......@@ -30,11 +35,11 @@ export PATH="$HOME/.heroku/node/bin:$HOME/.heroku/yarn/bin:$PATH:$HOME/bin:$HOME
export NODE_HOME="$HOME/.heroku/node"
export NODE_ENV=${NODE_ENV:-production}
calculate_concurrency
export MEMORY_AVAILABLE=${MEMORY_AVAILABLE:-$(detect_memory 512)}
export WEB_MEMORY=${WEB_MEMORY:-512}
export MEMORY_AVAILABLE=$MEMORY_AVAILABLE
export WEB_MEMORY=$WEB_MEMORY
export WEB_CONCURRENCY=$WEB_CONCURRENCY
# if the user hasn't set a value for WEB_CONCURRENCY we compute a reasonable value
export WEB_CONCURRENCY=${WEB_CONCURRENCY:-$(calculate_concurrency $MEMORY_AVAILABLE $WEB_MEMORY)}
if [ "$LOG_CONCURRENCY" = "true" ]; then
log_concurrency
......
......@@ -212,38 +212,45 @@ testBuildWithUserCacheDirectoriesCamel() {
assertCapturedSuccess
}
testConcurrencyExplicit() {
LOG_CONCURRENCY=true MEMORY_AVAILABLE=1024 WEB_MEMORY=256 WEB_CONCURRENCY=3 capture $(pwd)/profile/nodejs.sh
assertCaptured "Detected 1024 MB available memory, 256 MB limit per process (WEB_MEMORY)"
assertCaptured "Recommending WEB_CONCURRENCY=3"
assertCapturedSuccess
}
testConcurrency1X() {
LOG_CONCURRENCY=true MEMORY_AVAILABLE=512 capture $(pwd)/profile/nodejs.sh
assertCaptured "Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)"
assertCaptured "Recommending WEB_CONCURRENCY=1"
assertCaptured "Recommending WEB_CONCURRENCY=01"
assertCapturedSuccess
}
testConcurrency2X() {
LOG_CONCURRENCY=true MEMORY_AVAILABLE=1024 capture $(pwd)/profile/nodejs.sh
assertCaptured "Detected 1024 MB available memory, 512 MB limit per process (WEB_MEMORY)"
assertCaptured "Recommending WEB_CONCURRENCY=2"
assertCaptured "Recommending WEB_CONCURRENCY=02"
assertCapturedSuccess
}
testConcurrencyPerformanceM() {
LOG_CONCURRENCY=true MEMORY_AVAILABLE=2560 capture $(pwd)/profile/nodejs.sh
assertCaptured "Detected 2560 MB available memory, 512 MB limit per process (WEB_MEMORY)"
assertCaptured "Recommending WEB_CONCURRENCY=5"
assertCaptured "Recommending WEB_CONCURRENCY=05"
assertCapturedSuccess
}
testConcurrencyPerformanceL() {
LOG_CONCURRENCY=true MEMORY_AVAILABLE=14336 capture $(pwd)/profile/nodejs.sh
assertCaptured "Detected 14336 MB available memory, 512 MB limit per process (WEB_MEMORY)"
assertCaptured "Recommending WEB_CONCURRENCY=28"
assertCaptured "Recommending WEB_CONCURRENCY=028"
assertCapturedSuccess
}
testConcurrencyCustomLimit() {
LOG_CONCURRENCY=true MEMORY_AVAILABLE=1024 WEB_MEMORY=256 capture $(pwd)/profile/nodejs.sh
assertCaptured "Detected 1024 MB available memory, 256 MB limit per process (WEB_MEMORY)"
assertCaptured "Recommending WEB_CONCURRENCY=4"
assertCaptured "Recommending WEB_CONCURRENCY=04"
assertCapturedSuccess
}
......
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