| 1 | import { dirname, extname, join } from 'node:path' |
| 2 | import { fileURLToPath } from 'node:url' |
| 3 | |
| 4 | import { availableParallelism, DynamicThreadPool } from 'poolifier' |
| 5 | |
| 6 | import { |
| 7 | type BodyPayload, |
| 8 | type WorkerData, |
| 9 | type WorkerResponse |
| 10 | } from './types.js' |
| 11 | |
| 12 | const workerFile = join( |
| 13 | dirname(fileURLToPath(import.meta.url)), |
| 14 | `worker${extname(fileURLToPath(import.meta.url))}` |
| 15 | ) |
| 16 | |
| 17 | export const requestHandlerPool = new DynamicThreadPool< |
| 18 | WorkerData<BodyPayload>, |
| 19 | WorkerResponse<BodyPayload> |
| 20 | >(1, availableParallelism(), workerFile, { |
| 21 | enableTasksQueue: true, |
| 22 | tasksQueueOptions: { |
| 23 | concurrency: 8 |
| 24 | }, |
| 25 | errorHandler: (e: Error) => { |
| 26 | console.error('Thread worker error:', e) |
| 27 | } |
| 28 | }) |