Skip to content

Commit

Permalink
Remove clustering, remove WEB_CONCURRENCY setting (github#24914)
Browse files Browse the repository at this point in the history
* Remove WEB_CONCURRENCY setting
  • Loading branch information
mikesurowiec authored Feb 3, 2022
1 parent 6a5bf8d commit 8037fb4
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 142 deletions.
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ ENV NODE_ENV production
# Whether to hide iframes, add warnings to external links
ENV AIRGAP false

# By default we typically don't want to run in clustered mode
ENV WEB_CONCURRENCY 1

# Preferred port for server.mjs
ENV PORT 4000

Expand Down
4 changes: 0 additions & 4 deletions azure-preview-env-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@
"name": "DEPLOYMENT_ENV",
"value": "azure"
},
{
"name": "WEB_CONCURRENCY",
"value": "1"
},
{
"name": "ENABLED_LANGUAGES",
"value": "en"
Expand Down
1 change: 0 additions & 1 deletion docker-compose.prod.tmpl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ services:
HYDRO_ENDPOINT: ${HYDRO_ENDPOINT}
HYDRO_SECRET: ${HYDRO_SECRET}
HAYSTACK_URL: ${HAYSTACK_URL}
WEB_CONCURRENCY: ${WEB_CONCURRENCY}
HEROKU_APP_NAME: ${HEROKU_APP_NAME}
ENABLED_LANGUAGES: ${ENABLED_LANGUAGES}
DEPLOYMENT_ENV: ${DEPLOYMENT_ENV}
Expand Down
25 changes: 0 additions & 25 deletions lib/prefix-stream-write.js

This file was deleted.

20 changes: 0 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"strip-html-comments": "^1.0.0",
"styled-components": "^5.3.3",
"swr": "1.1.2",
"throng": "^5.0.0",
"ts-dedent": "^2.2.0",
"unified": "^10.1.0",
"unist-util-visit": "^4.1.0",
Expand Down Expand Up @@ -193,7 +192,7 @@
"repository": "https://github.com/github/docs",
"scripts": {
"browser-test": "start-server-and-test browser-test-server 4001 browser-test-tests",
"browser-test-server": "cross-env NODE_ENV=production WEB_CONCURRENCY=1 PORT=4001 ENABLED_LANGUAGES=en,ja node server.mjs",
"browser-test-server": "cross-env NODE_ENV=production PORT=4001 ENABLED_LANGUAGES=en,ja node server.mjs",
"browser-test-tests": "cross-env BROWSER=1 NODE_OPTIONS=--experimental-vm-modules jest tests/browser/browser.js",
"build": "next build",
"debug": "cross-env NODE_ENV=development ENABLED_LANGUAGES='en,ja' nodemon --inspect server.mjs",
Expand All @@ -214,7 +213,7 @@
"sync-search": "cross-env NODE_OPTIONS='--max_old_space_size=8192' start-server-and-test sync-search-server 4002 sync-search-indices",
"sync-search-ghes-release": "cross-env GHES_RELEASE=1 start-server-and-test sync-search-server 4002 sync-search-indices",
"sync-search-indices": "script/sync-search-indices.js",
"sync-search-server": "cross-env NODE_ENV=production WEB_CONCURRENCY=1 PORT=4002 node server.mjs",
"sync-search-server": "cross-env NODE_ENV=production PORT=4002 node server.mjs",
"translation-check": "start-server-and-test translation-check-server 4002 translation-check-test",
"translation-check-server": "cross-env NODE_ENV=test PORT=4002 node server.mjs",
"translation-check-test": "script/i18n/test-html-pages.js",
Expand Down
91 changes: 5 additions & 86 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,20 @@ import dotenv from 'dotenv'
import './lib/feature-flags.js'
import './lib/check-node-version.js'
import './lib/handle-exceptions.js'
import throng from 'throng'
import os from 'os'
import portUsed from 'port-used'
import prefixStreamWrite from './lib/prefix-stream-write.js'
import createApp from './lib/app.js'
import warmServer from './lib/warm-server.js'
import http from 'http'
dotenv.config()
// Intentionally require these for both cluster primary and workers

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

// Start the server!
if (NODE_ENV === 'production') {
clusteredMain()
} else {
nonClusteredMain()
}

function clusteredMain() {
// Spin up a cluster!
throng({
master: setupPrimary,
worker: setupWorker,
count: calculateWorkerCount(),
})
}
async function main() {
if (NODE_ENV !== 'production') {
await checkPortAvailability()
}

async function nonClusteredMain() {
await checkPortAvailability()
await startServer()
}

Expand Down Expand Up @@ -69,68 +52,4 @@ async function startServer() {
.on('error', () => server.close())
}

// This function will only be run in the primary process
async function setupPrimary() {
process.on('beforeExit', () => {
console.log('Shutting down primary...')
console.log('Exiting!')
})

console.log('Starting up primary...')

await checkPortAvailability()
}

// IMPORTANT: This function will be run in a separate worker process!
async function setupWorker(id, disconnect) {
let exited = false

// Wrap stdout and stderr to include the worker ID as a static prefix
// console.log('hi') => '[worker.1]: hi'
const prefix = `[worker.${id}]: `
prefixStreamWrite(process.stdout, prefix)
prefixStreamWrite(process.stderr, prefix)

process.on('beforeExit', () => {
console.log('Exiting!')
})

process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)

console.log('Starting up worker...')

// Load the server in each worker process and share the port via sharding
await startServer()

function shutdown() {
if (exited) return
exited = true

console.log('Shutting down worker...')
disconnect()
}
}

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

const recommendedCount = parseInt(WEB_CONCURRENCY, 10)
const cpuCount = os.cpus().length

// Ensure the recommended count is AT LEAST 1 for safety
let workerCount = Math.max(recommendedCount || 1, 1)

// If WEB_CONCURRENCY value was configured to a valid number...
if (recommendedCount > 0) {
// Use the smaller value between the recommendation vs. the CPU count
workerCount = Math.min(workerCount, cpuCount)
} else if (NODE_ENV === 'production') {
// Else if in a deployed environment, default to the CPU count
workerCount = cpuCount
}

return workerCount
}
main()

0 comments on commit 8037fb4

Please sign in to comment.