3c5e31e34dc85c9583b29ba427d8cb66704cb686
1 import type { TransferListItem
} from
'node: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 { workerFile
, minWorkers
, maxWorkers
, ...poolOptions
} = options
24 const pool
= new DynamicThreadPool
<ThreadWorkerData
, ThreadWorkerResponse
>(
30 if (!fastify
.hasDecorator('pool')) {
31 fastify
.decorate('pool', pool
)
33 if (!fastify
.hasDecorator('execute')) {
37 data
?: ThreadWorkerData
,
39 transferList
?: TransferListItem
[]
40 ): Promise
<ThreadWorkerResponse
> =>
41 await pool
.execute(data
, name
, transferList
)
47 export const fastifyPoolifier
= fp(fastifyPoolifierPlugin
, {
49 name
: 'fastify-poolifier'