implement separate web workers #252

Merged
toast merged 2 commits from web-worker into main 2022-12-03 13:33:24 +00:00
Showing only changes of commit 6600f6e52e - Show all commits

View file

@ -141,12 +141,12 @@ async function connectDb(): Promise<void> {
async function spawnWorkers(clusterLimits: Required<Config['clusterLimits']>): Promise<void> {
const modes = ['web', 'queue'];
const total = modes.reduce((acc, mode) => acc + clusterLimits[mode], 0);
if (total > os.cpus().length) {
bootLogger.error(`configuration error: cluster limits total (${total}) must not exceed number of cores (${os.cpus().length})`);
process.exit(78);
const cpus = os.cpus().length;
for (const mode of modes.filter(mode => clusterLimits[mode] > cpus)) {
bootLogger.warn(`configuration warning: cluster limit for ${mode} exceeds number of cores (${cpus})`);
}
const total = modes.reduce((acc, mode) => acc + clusterLimits[mode], 0);
const workers = new Array(total);
workers.fill('web', 0, clusterLimits.web);
workers.fill('queue', clusterLimits.web);
toast marked this conversation as resolved
Review

This construction is a little odd and seems to me to be prone to off-by-ones.
I would write it as this:

const web = new Array(clusterLimits.web).map(() => spawnWorker('web'));
const queue = new Array(clusterLimits.queue).map(() => spawnWorker('queue'));
const all = [...web, ...queue]);
console.log(`Starting ${all.length} workers...`);
await Promise.all(all);
This construction is a little odd and seems to me to be prone to off-by-ones. I would write it as this: ```js const web = new Array(clusterLimits.web).map(() => spawnWorker('web')); const queue = new Array(clusterLimits.queue).map(() => spawnWorker('queue')); const all = [...web, ...queue]); console.log(`Starting ${all.length} workers...`); await Promise.all(all); ```
Review

Your example code wouldn't work because new Array(n) generates an array with n empty slots and map basically ignores those.

Your example code wouldn't work because `new Array(n)` generates an array with `n` empty slots and `map` basically ignores those.
Review

I've thought about it a bit and it's fine.
Could be a TODO for cleanup later, but this is fine and works as expected.

I've thought about it a bit and it's fine. Could be a TODO for cleanup later, but this is fine and works as expected.