766ef44ec3fdcbb5e1e11304340d8065d47e03c1
1 import type { TransferListItem
} from
'node:worker_threads'
3 import type { FastifyPluginCallback
} from
'fastify'
4 import fp from
'fastify-plugin'
5 import { availableParallelism
, DynamicThreadPool
} from
'poolifier'
8 type FastifyPoolifierOptions
,
13 const fastifyPoolifierPlugin
: FastifyPluginCallback
<FastifyPoolifierOptions
> = (
21 maxWorkers
: availableParallelism()
25 const { workerFile
, minWorkers
, maxWorkers
, ...poolOptions
} = options
26 const pool
= new DynamicThreadPool
<WorkerData
, WorkerResponse
>(
32 if (!fastify
.hasDecorator('pool')) {
33 fastify
.decorate('pool', pool
)
35 if (!fastify
.hasDecorator('execute')) {
41 transferList
?: TransferListItem
[]
42 ): Promise
<WorkerResponse
> => await pool
.execute(data
, name
, transferList
)
48 export const fastifyPoolifier
= fp(fastifyPoolifierPlugin
, {
50 name
: 'fastify-poolifier'