Skip to content

Commit

Permalink
Warm up workers before switching to them (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Jan 17, 2018
1 parent 244f274 commit 072d799
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/WorkerFarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ let shared = null;
class WorkerFarm extends Farm {
constructor(options) {
let opts = {
autoStart: true,
maxConcurrentWorkers: getNumWorkers()
};

Expand All @@ -18,6 +17,7 @@ class WorkerFarm extends Farm {
this.remoteWorker = this.promisifyWorker(this.setup(['init', 'run']));

this.started = false;
this.warmWorkers = 0;
this.init(options);
}

Expand All @@ -40,7 +40,7 @@ class WorkerFarm extends Farm {
this.started = false;

let promises = [];
for (let i = 0; i < this.activeChildren; i++) {
for (let i = 0; i < this.options.maxConcurrentWorkers; i++) {
promises.push(this.remoteWorker.init(options));
}

Expand All @@ -60,10 +60,19 @@ class WorkerFarm extends Farm {
// Child process workers are slow to start (~600ms).
// While we're waiting, just run on the main thread.
// This significantly speeds up startup time.
if (!this.started) {
return this.localWorker.run(...args);
} else {
if (this.started && this.warmWorkers >= this.activeChildren) {
return this.remoteWorker.run(...args);
} else {
// Workers have started, but are not warmed up yet.
// Send the job to a remote worker in the background,
// but use the result from the local worker - it will be faster.
if (this.started) {
this.remoteWorker.run(...args).then(() => {
this.warmWorkers++;
});
}

return this.localWorker.run(...args);
}
}

Expand Down

0 comments on commit 072d799

Please sign in to comment.