| 1 | import { dirname, extname, join } from 'node:path' |
| 2 | import { fileURLToPath } from 'node:url' |
| 3 | import type { MyData, MyResponse } from './worker' |
| 4 | import { |
| 5 | DynamicThreadPool, |
| 6 | FixedThreadPool, |
| 7 | availableParallelism |
| 8 | } from 'poolifier' |
| 9 | |
| 10 | const workerFile = join( |
| 11 | dirname(fileURLToPath(import.meta.url)), |
| 12 | `worker${extname(fileURLToPath(import.meta.url))}` |
| 13 | ) |
| 14 | |
| 15 | export const fixedPool = new FixedThreadPool<MyData, Promise<MyResponse>>( |
| 16 | availableParallelism(), |
| 17 | workerFile, |
| 18 | { |
| 19 | errorHandler: (e: Error) => { |
| 20 | console.error(e) |
| 21 | }, |
| 22 | onlineHandler: () => { |
| 23 | console.info('Worker is online') |
| 24 | } |
| 25 | } |
| 26 | ) |
| 27 | |
| 28 | export const dynamicPool = new DynamicThreadPool<MyData, Promise<MyResponse>>( |
| 29 | Math.floor(availableParallelism() / 2), |
| 30 | availableParallelism(), |
| 31 | workerFile, |
| 32 | { |
| 33 | errorHandler: (e: Error) => { |
| 34 | console.error(e) |
| 35 | }, |
| 36 | onlineHandler: () => { |
| 37 | console.info('Worker is online') |
| 38 | } |
| 39 | } |
| 40 | ) |