1 import { DynamicThreadPool
, availableParallelism
} from
'poolifier'
2 import { type FastifyPluginCallback
} from
'fastify'
3 import fp from
'fastify-plugin'
5 type FastifyPoolifierOptions
,
10 const fastifyPoolifierPlugin
: FastifyPluginCallback
<FastifyPoolifierOptions
> = (
18 maxWorkers
: availableParallelism()
22 const pool
= new DynamicThreadPool
<WorkerData
, WorkerResponse
>(
23 options
.minWorkers
as number,
24 options
.maxWorkers
as number,
28 if (!fastify
.hasDecorator('pool')) {
29 fastify
.decorate('pool', pool
)
31 if (!fastify
.hasDecorator('execute')) {
34 async (data
?: WorkerData
, name
?: string): Promise
<WorkerResponse
> =>
35 await pool
.execute(data
, name
)
41 export const fastifyPoolifier
= fp(fastifyPoolifierPlugin
, {
43 name
: 'fastify-poolifier'