85e22c12698cc8d3dbe2fb068c005e993ed8872f
1 import type { TransferListItem
} from
'worker_threads'
2 import { DynamicThreadPool
, availableParallelism
} from
'poolifier'
3 import { type FastifyPluginCallback
} from
'fastify'
4 import fp from
'fastify-plugin'
6 type FastifyPoolifierOptions
,
11 const fastifyPoolifierPlugin
: FastifyPluginCallback
<FastifyPoolifierOptions
> = (
19 maxWorkers
: availableParallelism()
23 const { workerFile
, minWorkers
, maxWorkers
, ...poolOptions
} = options
24 const pool
= new DynamicThreadPool
<WorkerData
, WorkerResponse
>(
30 if (!fastify
.hasDecorator('pool')) {
31 fastify
.decorate('pool', pool
)
33 if (!fastify
.hasDecorator('execute')) {
39 transferList
?: TransferListItem
[]
40 ): Promise
<WorkerResponse
> => await pool
.execute(data
, name
, transferList
)
46 export const fastifyPoolifier
= fp(fastifyPoolifierPlugin
, {
48 name
: 'fastify-poolifier'