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
,
8 type ThreadWorkerResponse
11 const fastifyPoolifierPlugin
: FastifyPluginCallback
<FastifyPoolifierOptions
> = (
19 maxWorkers
: availableParallelism()
23 const pool
= new DynamicThreadPool
<ThreadWorkerData
, ThreadWorkerResponse
>(
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')) {
36 data
?: ThreadWorkerData
,
38 transferList
?: TransferListItem
[]
39 ): Promise
<ThreadWorkerResponse
> =>
40 await pool
.execute(data
, name
, transferList
)
46 export const fastifyPoolifier
= fp(fastifyPoolifierPlugin
, {
48 name
: 'fastify-poolifier'