import type { AddressInfo } from 'node:net'
-import { dirname, extname, join } from 'node:path'
-import { fileURLToPath } from 'node:url'
-import { ClusterWorker, availableParallelism } from 'poolifier'
+import { ClusterWorker } from 'poolifier'
import Fastify from 'fastify'
import type { ClusterWorkerData, ClusterWorkerResponse } from './types.js'
import { fastifyPoolifier } from './fastify-poolifier.js'
logger: true
})
- const requestHandlerWorkerFile = join(
- dirname(fileURLToPath(import.meta.url)),
- `request-handler-worker${extname(fileURLToPath(import.meta.url))}`
- )
-
- await fastify.register(fastifyPoolifier, {
- workerFile: requestHandlerWorkerFile,
- maxWorkers: Math.round(availableParallelism() / 2),
- enableTasksQueue: true,
- tasksQueueOptions: {
- concurrency: 8
- },
- errorHandler: (e: Error) => {
- fastify.log.error('Thread worker error', e)
- }
- })
+ await fastify.register(fastifyPoolifier, workerData)
fastify.all('/api/echo', async request => {
return (await fastify.execute({ body: request.body }, 'echo')).body
`fastify-worker${extname(fileURLToPath(import.meta.url))}`
)
+const requestHandlerWorkerFile = join(
+ dirname(fileURLToPath(import.meta.url)),
+ `request-handler-worker${extname(fileURLToPath(import.meta.url))}`
+)
+
const pool = new FixedClusterPool<ClusterWorkerData, ClusterWorkerResponse>(
Math.round(availableParallelism() / 2),
fastifyWorkerFile,
{
onlineHandler: () => {
pool
- .execute({ port: 8080 })
+ .execute({
+ port: 8080,
+ workerFile: requestHandlerWorkerFile,
+ maxWorkers: Math.round(availableParallelism() / 2),
+ enableTasksQueue: true,
+ tasksQueueOptions: {
+ concurrency: 8
+ },
+ errorHandler: (e: Error) => {
+ console.error('Thread worker error', e)
+ }
+ })
.then(response => {
if (response.status) {
console.info(