b4369082833e5409823401a9c61608765d97138a
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 FastifyPoolifierOptions
,
13 const fastifyPoolifierPlugin
: FastifyPluginCallback
<FastifyPoolifierOptions
> = (
21 maxWorkers
: availableParallelism()
25 const { workerFile
, minWorkers
, maxWorkers
, ...poolOptions
} = options
26 const pool
= new DynamicThreadPool
<ThreadWorkerData
, ThreadWorkerResponse
>(
32 if (!fastify
.hasDecorator('pool')) {
33 fastify
.decorate('pool', pool
)
35 if (!fastify
.hasDecorator('execute')) {
39 data
?: ThreadWorkerData
,
41 transferList
?: readonly TransferListItem
[]
42 ): Promise
<ThreadWorkerResponse
> =>
43 await pool
.execute(data
, name
, transferList
)
49 export const fastifyPoolifier
= fp(fastifyPoolifierPlugin
, {
51 name
: 'fastify-poolifier'