33calculate_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
1827log_concurrency () {
@@ -32,7 +41,7 @@ detect_memory() {
3241
3342bound_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.
4956This is likely due to running the Heroku NodeJS buildpack on a non-Heroku
5057platform.
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
5864DETECTED=$( detect_memory 512)
5965export MEMORY_AVAILABLE=${MEMORY_AVAILABLE-$(bound_memory $DETECTED )}
6066export 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
6584if [[ " ${LOG_CONCURRENCY+isset} " && " $LOG_CONCURRENCY " == " true" ]]; then
6685 log_concurrency
0 commit comments