### Breaking Changes
-- `maxInactiveTime` default behavior is now changed, if you want to keep the old behavior set `killBehavior` to `KillBehaviors.HARD`.
+- `maxInactiveTime` on `ThreadWorker` default behavior is now changed, if you want to keep the old behavior set `killBehavior` to `KillBehaviors.HARD`.
_Find more details on our JSDoc._
+- `maxTasks` option on `FixedThreadPool` and `DynamicThreadPool` is now removed since is no more needed.
+
- We changed some internal structures, but you shouldn't be too affected by them as these are internal changes.
### Pool options types declaration merge
- `errorHandler` - A function that will listen for error event on each worker
- `onlineHandler` - A function that will listen for online event on each worker
- `exitHandler` - A function that will listen for exit event on each worker
-- `maxTasks` - This is just to avoid not useful warnings message, is used to set [maxListeners](https://nodejs.org/dist/latest-v12.x/docs/api/events.html#events_emitter_setmaxlisteners_n) on event emitters (workers are event emitters)
### `pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)`
const dynamicPool = new DynamicClusterPool(
size / 2,
size * 3,
- './benchmarks/internal/cluster/worker.js',
- {
- maxTasks: 10000
- }
+ './benchmarks/internal/cluster/worker.js'
)
async function dynamicClusterTest (
const fixedPool = new FixedClusterPool(
size,
- './benchmarks/internal/cluster/worker.js',
- {
- maxTasks: 10000
- }
+ './benchmarks/internal/cluster/worker.js'
)
async function fixedClusterTest (
const size = 30
-const dynamicPool = new DynamicThreadPool(size / 2, size * 3, './worker.js', {
- maxTasks: 10000
-})
+const dynamicPool = new DynamicThreadPool(size / 2, size * 3, './worker.js')
async function dynamicThreadTest (
{ tasks, workerData } = { tasks: 1, workerData: { proof: 'ok' } }
const size = 30
-const fixedPool = new FixedThreadPool(size, './worker.js', {
- maxTasks: 10000
-})
+const fixedPool = new FixedThreadPool(size, './worker.js')
async function fixedThreadTest (
{ tasks, workerData } = { tasks: 1, workerData: { proof: 'ok' } }
const dynamicPool = new DynamicThreadPool(
size,
size * 3,
- './workers/poolifier/function-to-bench-worker.js',
- {
- maxTasks: 10000
- }
+ './workers/poolifier/function-to-bench-worker.js'
)
async function run () {
const fixedPool = new FixedThreadPool(
size,
- './workers/poolifier/function-to-bench-worker.js',
- {
- maxTasks: 100000
- }
+ './workers/poolifier/function-to-bench-worker.js'
)
async function run () {
* A function that will listen for exit event on each worker.
*/
exitHandler?: ExitHandler<Worker>
- /**
- * This is just to avoid non-useful warning messages.
- *
- * Will be used to set `maxListeners` on event emitters (workers are event emitters).
- *
- * @default 1000
- * @see [Node events emitter.setMaxListeners(n)](https://nodejs.org/api/events.html#events_emitter_setmaxlisteners_n)
- */
- maxTasks?: number
/**
* The work choice strategy to use in this pool.
*/
*
* @param numberOfWorkers Number of workers that this pool should manage.
* @param filePath Path to the worker-file.
- * @param opts Options for the pool. Default: `{ maxTasks: 1000 }`
+ * @param opts Options for the pool.
*/
public constructor (
public readonly numberOfWorkers: number,
public readonly filePath: string,
- public readonly opts: PoolOptions<Worker> = { maxTasks: 1000 }
+ public readonly opts: PoolOptions<Worker>
) {
if (!this.isMain()) {
throw new Error('Cannot start a pool from a worker!')
* @param min Minimum number of workers which are always active.
* @param max Maximum number of workers that can be created by this pool.
* @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
- * @param opts Options for this dynamic cluster pool. Default: `{ maxTasks: 1000 }`
+ * @param opts Options for this dynamic cluster pool. Default: `{}`
*/
public constructor (
min: number,
public readonly max: number,
filePath: string,
- opts: ClusterPoolOptions = { maxTasks: 1000 }
+ opts: ClusterPoolOptions = {}
) {
super(min, filePath, opts)
}
*
* @param numberOfWorkers Number of workers for this pool.
* @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
- * @param opts Options for this fixed cluster pool. Default: `{ maxTasks: 1000 }`
+ * @param opts Options for this fixed cluster pool. Default: `{}`
*/
public constructor (
numberOfWorkers: number,
filePath: string,
- public readonly opts: ClusterPoolOptions = { maxTasks: 1000 }
+ public readonly opts: ClusterPoolOptions = {}
) {
super(numberOfWorkers, filePath, opts)
}
}
protected afterWorkerSetup (worker: Worker): void {
- // We will attach a listener for every task,
- // when task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size
- worker.setMaxListeners(this.opts.maxTasks ?? 1000)
+ // Listen worker messages.
this.registerWorkerMessageListener(worker, super.workerListener())
}
}
* @param min Minimum number of threads which are always active.
* @param max Maximum number of threads that can be created by this pool.
* @param filePath Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
- * @param opts Options for this dynamic thread pool. Default: `{ maxTasks: 1000 }`
+ * @param opts Options for this dynamic thread pool. Default: `{}`
*/
public constructor (
min: number,
public readonly max: number,
filePath: string,
- opts: PoolOptions<ThreadWorkerWithMessageChannel> = { maxTasks: 1000 }
+ opts: PoolOptions<ThreadWorkerWithMessageChannel> = {}
) {
super(min, filePath, opts)
}
*
* @param numberOfThreads Number of threads for this pool.
* @param filePath Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
- * @param opts Options for this fixed thread pool. Default: `{ maxTasks: 1000 }`
+ * @param opts Options for this fixed thread pool. Default: `{}`
*/
public constructor (
numberOfThreads: number,
filePath: string,
- opts: PoolOptions<ThreadWorkerWithMessageChannel> = { maxTasks: 1000 }
+ opts: PoolOptions<ThreadWorkerWithMessageChannel> = {}
) {
super(numberOfThreads, filePath, opts)
}
worker.postMessage({ parent: port1 }, [port1])
worker.port1 = port1
worker.port2 = port2
- // We will attach a listener for every task,
- // when the task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size.
- worker.port2.setMaxListeners(this.opts.maxTasks ?? 1000)
+ // Listen worker messages.
this.registerWorkerMessageListener(worker, super.workerListener())
}
}
it('Simulate worker not found during increaseWorkersTask', () => {
const pool = new StubPoolWithTasksMapClear(
1,
- './tests/worker-files/thread/testWorker.js',
- {
- errorHandler: e => console.error(e)
- }
+ './tests/worker-files/thread/testWorker.js'
)
// Simulate worker not found.
pool.removeAllWorker()
const { FixedClusterPool } = require('../../../lib/index')
const TestUtils = require('../../test-utils')
const numberOfWorkers = 10
-const maxTasks = 500
const pool = new FixedClusterPool(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
)
const asyncPool = new FixedClusterPool(
1,
- './tests/worker-files/cluster/asyncWorker.js',
- {
- maxTasks: maxTasks
- }
+ './tests/worker-files/cluster/asyncWorker.js'
)
describe('Fixed cluster pool test suite', () => {
expect(usedTime).toBeGreaterThanOrEqual(2000)
})
- it('Verify that maxTasks is set properly', async () => {
- const worker = asyncPool.chooseWorker()
- expect(worker.getMaxListeners()).toBe(maxTasks)
- })
-
it('Shutdown test', async () => {
const exitPromise = TestUtils.waitExits(pool, numberOfWorkers)
await pool.destroy()
min,
max,
'./tests/worker-files/thread/testWorker.js',
- {
- maxTasks: 1000,
- workerChoiceStrategy: 'UNKNOWN_STRATEGY'
- }
+ { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
)
).toThrowError(
new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
const { FixedThreadPool } = require('../../../lib/index')
const TestUtils = require('../../test-utils')
const numberOfThreads = 10
-const maxTasks = 400
const pool = new FixedThreadPool(
numberOfThreads,
'./tests/worker-files/thread/testWorker.js',
)
const asyncPool = new FixedThreadPool(
1,
- './tests/worker-files/thread/asyncWorker.js',
- { maxTasks: maxTasks }
+ './tests/worker-files/thread/asyncWorker.js'
)
describe('Fixed thread pool test suite', () => {
expect(usedTime).toBeGreaterThanOrEqual(2000)
})
- it('Verify that maxTasks is set properly', async () => {
- const worker = asyncPool.chooseWorker()
- expect(worker.port2.getMaxListeners()).toBe(maxTasks)
- })
-
it('Shutdown test', async () => {
const exitPromise = TestUtils.waitExits(pool, numberOfThreads)
await pool.destroy()