Skip to content

Commit 94362e6

Browse files
authored
Adjust worker count calculation to allow local clustering with WEB_CONCURRENCY (#18366)
* Adjust worker count calculation to allow local clustering with WEB_CONCURRENCY * Only honor WEB_CONCURRENCY if it parses to a number > 0
1 parent 62a8891 commit 94362e6

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

server.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,23 @@ async function setupWorker (id, disconnect) {
9191
}
9292

9393
function calculateWorkerCount () {
94-
// Heroku's recommended WEB_CONCURRENCY count based on the WEB_MEMORY config
94+
// Heroku's recommended WEB_CONCURRENCY count based on the WEB_MEMORY config,
95+
// or explicitly configured by us
9596
const { WEB_CONCURRENCY } = process.env
9697

97-
const recommendedCount = parseInt(WEB_CONCURRENCY, 10) || 1
98+
const recommendedCount = parseInt(WEB_CONCURRENCY, 10)
9899
const cpuCount = os.cpus().length
99100

100101
// Ensure the recommended count is AT LEAST 1 for safety
101-
let workerCount = Math.max(recommendedCount, 1)
102-
103-
// Let's do some math...
104-
// If in a deployed environment...
105-
if (NODE_ENV === 'production') {
106-
// If WEB_MEMORY or WEB_CONCURRENCY values were configured in Heroku, use
107-
// the smaller value between their recommendation vs. the CPU count
108-
if (WEB_CONCURRENCY) {
109-
workerCount = Math.min(recommendedCount, cpuCount)
110-
} else {
111-
workerCount = cpuCount
112-
}
102+
let workerCount = Math.max(recommendedCount || 1, 1)
103+
104+
// If WEB_CONCURRENCY value was configured to a valid number...
105+
if (recommendedCount > 0) {
106+
// Use the smaller value between the recommendation vs. the CPU count
107+
workerCount = Math.min(workerCount, cpuCount)
108+
} else if (NODE_ENV === 'production') {
109+
// Else if in a deployed environment, default to the CPU count
110+
workerCount = cpuCount
113111
}
114112

115113
return workerCount

0 commit comments

Comments
 (0)