-
-
Notifications
You must be signed in to change notification settings - Fork 44
Add cluster support to multiply outbound write throughput #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
d2ecab0
feat(cluster): add multi-worker cluster support for outbound write th…
stilliard 9869524
fix(cluster): prevent fast-crash respawn loop and restore default app…
stilliard b96417c
fix(cluster): fall back to default workers for non-positive WEB_CONCU…
stilliard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| const cluster = require('cluster'); | ||
|
|
||
| if (cluster.isPrimary) { | ||
| const requested = parseInt(process.env.WEB_CONCURRENCY, 10); | ||
| const workers = Number.isInteger(requested) && requested > 0 ? requested : 4; | ||
| console.log(`Primary ${process.pid} starting ${workers} workers`); | ||
| for (let i = 0; i < workers; i++) fork(); | ||
|
|
||
| // resurrect a worker if it dies, unless we're shutting down, also track fast crashes and exit if too many happen in a row | ||
| let shuttingDown = false; | ||
| let fastCrashes = 0; | ||
| cluster.on('exit', (worker, code, signal) => { | ||
| if (shuttingDown) return; | ||
| const ranForMs = Date.now() - worker.startedAt; | ||
| if (ranForMs < 5000) { | ||
| fastCrashes++; | ||
| console.error(`Worker ${worker.process.pid} died after ${ranForMs}ms (${signal || code}) [fast-crash ${fastCrashes}/${workers}]`); | ||
| if (fastCrashes >= workers) { | ||
| console.error('Too many fast worker crashes; exiting for the service manager to restart.'); | ||
| process.exit(1); | ||
| } | ||
| } else { | ||
| fastCrashes = 0; // a healthy run clears the streak | ||
| console.log(`Worker ${worker.process.pid} died after ${Math.round(ranForMs / 1000)}s (${signal || code}); respawning`); | ||
| } | ||
| fork(); | ||
| }); | ||
|
|
||
| for (const sig of ['SIGTERM', 'SIGINT']) { | ||
| process.on(sig, () => { | ||
| shuttingDown = true; | ||
| for (const w of Object.values(cluster.workers)) w.kill(sig); | ||
| }); | ||
| } | ||
| } else { | ||
| // equivalent to `probot run ./index.js` (app path passed as a positional arg) | ||
| require('probot').run([process.argv[0], process.argv[1], './index.js']); | ||
| } | ||
|
|
||
| function fork() { | ||
| const worker = cluster.fork(); | ||
| worker.startedAt = Date.now(); | ||
| return worker; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.