## [Unreleased]
+### Added
+
+- Add safe helper `availableParallelism` to help sizing the pool.
+
### Fixed
- Ensure message handler is only registered in worker.
```js
'use strict'
-const { DynamicThreadPool, FixedThreadPool, PoolEvents } = require('poolifier')
+const { DynamicThreadPool, FixedThreadPool, PoolEvents, availableParallelism } = require('poolifier')
// a fixed worker-threads pool
-const pool = new FixedThreadPool(15, './yourWorker.js', {
+const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', {
errorHandler: e => console.error(e),
onlineHandler: () => console.info('worker is online')
})
pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy'))
// or a dynamic worker-threads pool
-const pool = new DynamicThreadPool(10, 100, './yourWorker.js', {
+const pool = new DynamicThreadPool(availableParallelism() / 2, availableParallelism(), './yourWorker.js', {
errorHandler: e => console.error(e),
onlineHandler: () => console.info('worker is online')
})
-const { DynamicThreadPool, PoolEvents } = require('poolifier')
-let resolved = 0
+const {
+ DynamicThreadPool,
+ PoolEvents,
+ availableParallelism
+} = require('poolifier')
+
+const pool = new DynamicThreadPool(
+ availableParallelism() / 2,
+ availableParallelism(),
+ './yourWorker.js',
+ {
+ errorHandler: e => console.error(e),
+ onlineHandler: () => console.info('worker is online')
+ }
+)
let poolFull = 0
let poolBusy = 0
-const pool = new DynamicThreadPool(10, 20, './yourWorker.js', {
- errorHandler: e => console.error(e),
- onlineHandler: () => console.info('worker is online')
-})
pool.emitter.on(PoolEvents.full, () => poolFull++)
pool.emitter.on(PoolEvents.busy, () => poolBusy++)
+let resolved = 0
const start = performance.now()
const iterations = 1000
for (let i = 1; i <= iterations; i++) {
-const { FixedThreadPool, PoolEvents } = require('poolifier')
-let resolved = 0
-let poolBusy = 0
-const pool = new FixedThreadPool(15, './yourWorker.js', {
+const {
+ FixedThreadPool,
+ PoolEvents,
+ availableParallelism
+} = require('poolifier')
+
+const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', {
errorHandler: e => console.error(e),
onlineHandler: () => console.info('worker is online')
})
+let poolBusy = 0
pool.emitter.on(PoolEvents.busy, () => poolBusy++)
+let resolved = 0
const start = performance.now()
const iterations = 1000
for (let i = 1; i <= iterations; i++) {
-const { FixedThreadPool } = require('poolifier')
-const pool = new FixedThreadPool(15, './multiFunctionWorker.js', {
- errorHandler: e => console.error(e),
- onlineHandler: () => console.info('worker is online')
-})
+const { FixedThreadPool, availableParallelism } = require('poolifier')
+
+const pool = new FixedThreadPool(
+ availableParallelism(),
+ './multiFunctionWorker.js',
+ {
+ errorHandler: e => console.error(e),
+ onlineHandler: () => console.info('worker is online')
+ }
+)
pool
.execute({ text: 'hello' }, 'fn0')
import { join } from 'path'
import type { MyData, MyResponse } from './worker'
-import { DynamicThreadPool, FixedThreadPool } from 'poolifier'
+import {
+ DynamicThreadPool,
+ FixedThreadPool,
+ availableParallelism
+} from 'poolifier'
export const fixedPool = new FixedThreadPool<MyData, Promise<MyResponse>>(
- 8,
+ availableParallelism(),
join(__dirname, 'worker.js'),
{
errorHandler: (e: Error) => {
)
export const dynamicPool = new DynamicThreadPool<MyData, Promise<MyResponse>>(
- 2,
- 8,
+ availableParallelism() / 2,
+ availableParallelism(),
join(__dirname, 'worker.js'),
{
errorHandler: (e: Error) => {
const { expect } = require('expect')
-const { isPlainObject, median } = require('../lib/utils')
+const { isPlainObject, median, availableParallelism } = require('../lib/utils')
const {
isKillBehavior,
KillBehaviors
expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false)
expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false)
})
+
+ it('Verify availableParallelism() behavior', () => {
+ expect(typeof availableParallelism() === 'number').toBe(true)
+ })
})