Skip to content

Commit d3b484a

Browse files
authored
Merge pull request heroku#931 from heroku/refactor-web-concurrency
Refactor $WEB_CONCURRENCY handling
2 parents 23572a2 + 52a8d7f commit d3b484a

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
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: 33 additions & 14 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() {
@@ -32,7 +41,7 @@ detect_memory() {
3241

3342
bound_memory() {
3443
local detected=$1
35-
local detected max_detected_memory=14336
44+
local max_detected_memory=14336
3645

3746
# The hardcoded value is 16GB of memory
3847
if (( detected > max_detected_memory )); then
@@ -42,25 +51,35 @@ bound_memory() {
4251
fi
4352
}
4453

45-
warn_bad_web_concurrency() {
46-
local concurrency=$((MEMORY_AVAILABLE/WEB_MEMORY))
47-
if [ "$concurrency" -gt "200" ]; then
48-
echo "Could not determine a reasonable value for WEB_CONCURRENCY.
54+
warn_high_web_concurrency() {
55+
echo "Could not determine a reasonable value for WEB_CONCURRENCY.
4956
This is likely due to running the Heroku NodeJS buildpack on a non-Heroku
5057
platform.
5158
52-
WEB_CONCURRENCY has been set to 1. Please review whether this value is
53-
appropriate for your application."
54-
echo ""
55-
fi
59+
WEB_CONCURRENCY has been set to ${1}. Please review whether this value is
60+
appropriate for your application.
61+
"
5662
}
5763

5864
DETECTED=$(detect_memory 512)
5965
export MEMORY_AVAILABLE=${MEMORY_AVAILABLE-$(bound_memory $DETECTED)}
6066
export WEB_MEMORY=${WEB_MEMORY-512}
61-
export WEB_CONCURRENCY=$(calculate_concurrency $MEMORY_AVAILABLE $WEB_MEMORY)
62-
63-
warn_bad_web_concurrency
67+
WEB_CONCURRENCY=${WEB_CONCURRENCY-$(calculate_concurrency "$MEMORY_AVAILABLE" "$WEB_MEMORY")}
68+
validated_concurrency=$(validate_concurrency "$WEB_CONCURRENCY")
69+
case $? in # validate_concurrency exit code indicates result
70+
1)
71+
# too low
72+
export WEB_CONCURRENCY=$validated_concurrency
73+
;;
74+
2)
75+
# too high
76+
warn_high_web_concurrency "$validated_concurrency" "$WEB_CONCURRENCY"
77+
export WEB_CONCURRENCY=$validated_concurrency
78+
;;
79+
0)
80+
export WEB_CONCURRENCY
81+
;;
82+
esac
6483

6584
if [[ "${LOG_CONCURRENCY+isset}" && "$LOG_CONCURRENCY" == "true" ]]; then
6685
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)