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 pool
= new DynamicThreadPool
<WorkerData
, WorkerResponse
>(
24 options
.minWorkers
as number,
25 options
.maxWorkers
as number,
29 if (!fastify
.hasDecorator('pool')) {
30 fastify
.decorate('pool', pool
)
32 if (!fastify
.hasDecorator('execute')) {
38 transferList
?: TransferListItem
[]
39 ): Promise
<WorkerResponse
> => await pool
.execute(data
, name
, transferList
)
45 export const fastifyPoolifier
= fp(fastifyPoolifierPlugin
, {
47 name
: 'fastify-poolifier'