diff --git a/README.md b/README.md index 37c079c..bc4dcfb 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,10 @@ export default ({ a, b }) => { We have a similar API to Piscina, so for more information, you can read Piscina's detailed [documentation](https://github.com/piscinajs/piscina#piscina---the-nodejs-worker-pool) and apply the same techniques here. +###### Additional Options + +- `isolateWorkers`: Default to `false`. Always starts with a fresh worker when running tasks to isolate the environment. + ## Credits [The Vitest team](https://vitest.dev/) for giving me the chance of creating and maintaing this project for vitest. diff --git a/src/index.ts b/src/index.ts index 58db252..bae8d54 100644 --- a/src/index.ts +++ b/src/index.ts @@ -130,6 +130,7 @@ interface Options { workerData?: any taskQueue?: TaskQueue trackUnmanagedFds?: boolean + isolateWorkers?: boolean } interface FilledOptions extends Options { @@ -774,6 +775,9 @@ class ThreadPool { } filename = maybeFileURLToPath(filename) + // Look for a Worker with a minimum number of tasks it is currently running. + let workerInfo: WorkerInfo | null = this.workers.findAvailable() + let resolve: (result: any) => void let reject: (err: Error) => void // eslint-disable-next-line @@ -788,6 +792,9 @@ class ThreadPool { name, (err: Error | null, result: any) => { this.completed++ + if (workerInfo && this.options.isolateWorkers) { + this._removeWorker(workerInfo) + } if (err !== null) { reject(err) } else { @@ -842,9 +849,6 @@ class ThreadPool { return ret } - // Look for a Worker with a minimum number of tasks it is currently running. - let workerInfo: WorkerInfo | null = this.workers.findAvailable() - // If we want the ability to abort this task, use only workers that have // no running tasks. if (workerInfo !== null && workerInfo.currentUsage() > 0 && signal) { @@ -877,6 +881,7 @@ class ThreadPool { taskInfo.started = now workerInfo.postTask(taskInfo) this._maybeDrain() + return ret } diff --git a/test/fixtures/isolated.js b/test/fixtures/isolated.js new file mode 100644 index 0000000..77c1e9a --- /dev/null +++ b/test/fixtures/isolated.js @@ -0,0 +1,3 @@ +let count = 0 + +export default () => count++ diff --git a/test/simple.test.ts b/test/simple.test.ts index 7296e57..ac4eb60 100644 --- a/test/simple.test.ts +++ b/test/simple.test.ts @@ -156,3 +156,23 @@ test('can destroy pool while tasks are running', async () => { /Terminating worker thread/ ) }) + +test('isolateWorkers: false', async () => { + const pool = new Tinypool({ + filename: resolve(__dirname, 'fixtures/isolated.js'), + isolateWorkers: false, + }) + expect(await pool.run({})).toBe(1) + expect(await pool.run({})).toBe(2) + expect(await pool.run({})).toBe(3) +}) + +test('isolateWorkers: true', async () => { + const pool = new Tinypool({ + filename: resolve(__dirname, 'fixtures/isolated.js'), + isolateWorkers: true, + }) + expect(await pool.run({})).toBe(1) + expect(await pool.run({})).toBe(1) + expect(await pool.run({})).toBe(1) +})