|
3 | 3 | calculate_concurrency() { |
4 | 4 | local available=$1 |
5 | 5 | local web_memory=$2 |
6 | | - local concurrency |
7 | 6 |
|
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 | + |
9 | 14 | if (( concurrency < 1 )); then |
10 | 15 | concurrency=1 |
| 16 | + ret=1 |
11 | 17 | elif (( concurrency > 200 )); then |
12 | 18 | # Ex: This will happen on Dokku on DO |
13 | 19 | concurrency=1 |
| 20 | + ret=2 |
14 | 21 | fi |
| 22 | + |
15 | 23 | echo "$concurrency" |
| 24 | + return $ret |
16 | 25 | } |
17 | 26 |
|
18 | 27 | log_concurrency() { |
@@ -43,24 +52,32 @@ bound_memory() { |
43 | 52 | } |
44 | 53 |
|
45 | 54 | 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? |
48 | 56 | echo "Could not determine a reasonable value for WEB_CONCURRENCY. |
49 | 57 | This is likely due to running the Heroku NodeJS buildpack on a non-Heroku |
50 | 58 | platform. |
51 | 59 |
|
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 | +" |
55 | 63 | fi |
56 | 64 | } |
57 | 65 |
|
58 | 66 | DETECTED=$(detect_memory 512) |
59 | 67 | export MEMORY_AVAILABLE=${MEMORY_AVAILABLE-$(bound_memory $DETECTED)} |
60 | 68 | 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 |
64 | 81 |
|
65 | 82 | if [[ "${LOG_CONCURRENCY+isset}" && "$LOG_CONCURRENCY" == "true" ]]; then |
66 | 83 | log_concurrency |
|
0 commit comments