Skip to content

Commit 8037fb4

Browse files
authoredFeb 3, 2022
Remove clustering, remove WEB_CONCURRENCY setting (github#24914)
* Remove WEB_CONCURRENCY setting
1 parent 6a5bf8d commit 8037fb4

7 files changed

+7
-142
lines changed
 

‎Dockerfile

-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ ENV NODE_ENV production
7676
# Whether to hide iframes, add warnings to external links
7777
ENV AIRGAP false
7878

79-
# By default we typically don't want to run in clustered mode
80-
ENV WEB_CONCURRENCY 1
81-
8279
# Preferred port for server.mjs
8380
ENV PORT 4000
8481

‎azure-preview-env-template.json

-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@
8383
"name": "DEPLOYMENT_ENV",
8484
"value": "azure"
8585
},
86-
{
87-
"name": "WEB_CONCURRENCY",
88-
"value": "1"
89-
},
9086
{
9187
"name": "ENABLED_LANGUAGES",
9288
"value": "en"

‎docker-compose.prod.tmpl.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ services:
1212
HYDRO_ENDPOINT: ${HYDRO_ENDPOINT}
1313
HYDRO_SECRET: ${HYDRO_SECRET}
1414
HAYSTACK_URL: ${HAYSTACK_URL}
15-
WEB_CONCURRENCY: ${WEB_CONCURRENCY}
1615
HEROKU_APP_NAME: ${HEROKU_APP_NAME}
1716
ENABLED_LANGUAGES: ${ENABLED_LANGUAGES}
1817
DEPLOYMENT_ENV: ${DEPLOYMENT_ENV}

‎lib/prefix-stream-write.js

-25
This file was deleted.

‎package-lock.json

-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
"strip-html-comments": "^1.0.0",
8888
"styled-components": "^5.3.3",
8989
"swr": "1.1.2",
90-
"throng": "^5.0.0",
9190
"ts-dedent": "^2.2.0",
9291
"unified": "^10.1.0",
9392
"unist-util-visit": "^4.1.0",
@@ -193,7 +192,7 @@
193192
"repository": "https://github.com/github/docs",
194193
"scripts": {
195194
"browser-test": "start-server-and-test browser-test-server 4001 browser-test-tests",
196-
"browser-test-server": "cross-env NODE_ENV=production WEB_CONCURRENCY=1 PORT=4001 ENABLED_LANGUAGES=en,ja node server.mjs",
195+
"browser-test-server": "cross-env NODE_ENV=production PORT=4001 ENABLED_LANGUAGES=en,ja node server.mjs",
197196
"browser-test-tests": "cross-env BROWSER=1 NODE_OPTIONS=--experimental-vm-modules jest tests/browser/browser.js",
198197
"build": "next build",
199198
"debug": "cross-env NODE_ENV=development ENABLED_LANGUAGES='en,ja' nodemon --inspect server.mjs",
@@ -214,7 +213,7 @@
214213
"sync-search": "cross-env NODE_OPTIONS='--max_old_space_size=8192' start-server-and-test sync-search-server 4002 sync-search-indices",
215214
"sync-search-ghes-release": "cross-env GHES_RELEASE=1 start-server-and-test sync-search-server 4002 sync-search-indices",
216215
"sync-search-indices": "script/sync-search-indices.js",
217-
"sync-search-server": "cross-env NODE_ENV=production WEB_CONCURRENCY=1 PORT=4002 node server.mjs",
216+
"sync-search-server": "cross-env NODE_ENV=production PORT=4002 node server.mjs",
218217
"translation-check": "start-server-and-test translation-check-server 4002 translation-check-test",
219218
"translation-check-server": "cross-env NODE_ENV=test PORT=4002 node server.mjs",
220219
"translation-check-test": "script/i18n/test-html-pages.js",

‎server.mjs

+5-86
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,20 @@ import dotenv from 'dotenv'
22
import './lib/feature-flags.js'
33
import './lib/check-node-version.js'
44
import './lib/handle-exceptions.js'
5-
import throng from 'throng'
6-
import os from 'os'
75
import portUsed from 'port-used'
8-
import prefixStreamWrite from './lib/prefix-stream-write.js'
96
import createApp from './lib/app.js'
107
import warmServer from './lib/warm-server.js'
118
import http from 'http'
129
dotenv.config()
13-
// Intentionally require these for both cluster primary and workers
1410

1511
const { PORT, NODE_ENV } = process.env
1612
const port = Number(PORT) || 4000
1713

18-
// Start the server!
19-
if (NODE_ENV === 'production') {
20-
clusteredMain()
21-
} else {
22-
nonClusteredMain()
23-
}
24-
25-
function clusteredMain() {
26-
// Spin up a cluster!
27-
throng({
28-
master: setupPrimary,
29-
worker: setupWorker,
30-
count: calculateWorkerCount(),
31-
})
32-
}
14+
async function main() {
15+
if (NODE_ENV !== 'production') {
16+
await checkPortAvailability()
17+
}
3318

34-
async function nonClusteredMain() {
35-
await checkPortAvailability()
3619
await startServer()
3720
}
3821

@@ -69,68 +52,4 @@ async function startServer() {
6952
.on('error', () => server.close())
7053
}
7154

72-
// This function will only be run in the primary process
73-
async function setupPrimary() {
74-
process.on('beforeExit', () => {
75-
console.log('Shutting down primary...')
76-
console.log('Exiting!')
77-
})
78-
79-
console.log('Starting up primary...')
80-
81-
await checkPortAvailability()
82-
}
83-
84-
// IMPORTANT: This function will be run in a separate worker process!
85-
async function setupWorker(id, disconnect) {
86-
let exited = false
87-
88-
// Wrap stdout and stderr to include the worker ID as a static prefix
89-
// console.log('hi') => '[worker.1]: hi'
90-
const prefix = `[worker.${id}]: `
91-
prefixStreamWrite(process.stdout, prefix)
92-
prefixStreamWrite(process.stderr, prefix)
93-
94-
process.on('beforeExit', () => {
95-
console.log('Exiting!')
96-
})
97-
98-
process.on('SIGTERM', shutdown)
99-
process.on('SIGINT', shutdown)
100-
101-
console.log('Starting up worker...')
102-
103-
// Load the server in each worker process and share the port via sharding
104-
await startServer()
105-
106-
function shutdown() {
107-
if (exited) return
108-
exited = true
109-
110-
console.log('Shutting down worker...')
111-
disconnect()
112-
}
113-
}
114-
115-
function calculateWorkerCount() {
116-
// Heroku's recommended WEB_CONCURRENCY count based on the WEB_MEMORY config,
117-
// or explicitly configured by us
118-
const { WEB_CONCURRENCY } = process.env
119-
120-
const recommendedCount = parseInt(WEB_CONCURRENCY, 10)
121-
const cpuCount = os.cpus().length
122-
123-
// Ensure the recommended count is AT LEAST 1 for safety
124-
let workerCount = Math.max(recommendedCount || 1, 1)
125-
126-
// If WEB_CONCURRENCY value was configured to a valid number...
127-
if (recommendedCount > 0) {
128-
// Use the smaller value between the recommendation vs. the CPU count
129-
workerCount = Math.min(workerCount, cpuCount)
130-
} else if (NODE_ENV === 'production') {
131-
// Else if in a deployed environment, default to the CPU count
132-
workerCount = cpuCount
133-
}
134-
135-
return workerCount
136-
}
55+
main()

0 commit comments

Comments
 (0)