Skip to content

Commit 1c1c5ca

Browse files
committed
split WEB_CONCURRENCY validation from calculation
1 parent 8770f1b commit 1c1c5ca

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Node.js Buildpack Changelog
22

33
## main
4+
- Refactor $WEB_CONCURRENCY logic ([#931](https://github.com/heroku/heroku-buildpack-nodejs/pull/931))
45

56
## v185 (2021-06-03)
67
- Drop Heroku-16 from CI test matrix ([#920](https://github.com/heroku/heroku-buildpack-nodejs/pull/920))

profile/WEB_CONCURRENCY.sh

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,25 @@
33
calculate_concurrency() {
44
local available=$1
55
local web_memory=$2
6-
local concurrency
76

8-
concurrency=${WEB_CONCURRENCY-$(($available/$web_memory))}
7+
echo $(($available/$web_memory))
8+
}
9+
10+
validate_concurrency() {
11+
local concurrency=$1
12+
local ret=0
13+
914
if (( concurrency < 1 )); then
1015
concurrency=1
16+
ret=1
1117
elif (( concurrency > 200 )); then
1218
# Ex: This will happen on Dokku on DO
1319
concurrency=1
20+
ret=2
1421
fi
22+
1523
echo "$concurrency"
24+
return $ret
1625
}
1726

1827
log_concurrency() {
@@ -43,24 +52,32 @@ bound_memory() {
4352
}
4453

4554
warn_bad_web_concurrency() {
46-
local concurrency=$((MEMORY_AVAILABLE/WEB_MEMORY))
47-
if [ "$concurrency" -gt "200" ]; then
55+
if (( $2 > 200 )); then # FIXME: should this even be here? or should the case further down not call for $?==1 maybe?
4856
echo "Could not determine a reasonable value for WEB_CONCURRENCY.
4957
This is likely due to running the Heroku NodeJS buildpack on a non-Heroku
5058
platform.
5159
52-
WEB_CONCURRENCY has been set to 1. Please review whether this value is
53-
appropriate for your application."
54-
echo ""
60+
WEB_CONCURRENCY has been set to ${1}. Please review whether this value is
61+
appropriate for your application.
62+
"
5563
fi
5664
}
5765

5866
DETECTED=$(detect_memory 512)
5967
export MEMORY_AVAILABLE=${MEMORY_AVAILABLE-$(bound_memory $DETECTED)}
6068
export WEB_MEMORY=${WEB_MEMORY-512}
61-
export WEB_CONCURRENCY=$(calculate_concurrency $MEMORY_AVAILABLE $WEB_MEMORY)
62-
63-
warn_bad_web_concurrency
69+
WEB_CONCURRENCY=${WEB_CONCURRENCY-$(calculate_concurrency "$MEMORY_AVAILABLE" "$WEB_MEMORY")}
70+
validated_concurrency=$(validate_concurrency "$WEB_CONCURRENCY")
71+
case $? in
72+
[1-2])
73+
# too high or low
74+
warn_bad_web_concurrency "$validated_concurrency" "$WEB_CONCURRENCY"
75+
export WEB_CONCURRENCY=$validated_concurrency
76+
;;
77+
0)
78+
export WEB_CONCURRENCY
79+
;;
80+
esac
6481

6582
if [[ "${LOG_CONCURRENCY+isset}" && "$LOG_CONCURRENCY" == "true" ]]; then
6683
log_concurrency

test/unit

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ testWebConcurrencyProfileScript() {
334334
assertEquals "28" "$(calculate_concurrency 14336 512)"
335335

336336
# In case some very large memory available value gets passed in
337-
assertEquals "1" "$(calculate_concurrency 103401 512)"
337+
assertEquals "1" "$(validate_concurrency $(calculate_concurrency 103401 512))"
338338
# of if web memory is set really low
339-
assertEquals "1" "$(calculate_concurrency 512 1)"
339+
assertEquals "1" "$(validate_concurrency $(calculate_concurrency 512 1))"
340340
}
341341

342342
isUUID() {

0 commit comments

Comments
 (0)