refactor: move worker_threads pool init in main method in fastify-hybrid
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 13 Aug 2023 16:10:13 +0000 (18:10 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 13 Aug 2023 16:10:13 +0000 (18:10 +0200)
example

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts
examples/typescript/http-server-pool/fastify-hybrid/src/main.ts
examples/typescript/http-server-pool/fastify-hybrid/src/types.ts
examples/typescript/http-server-pool/fastify-worker_threads/src/types.ts

index 1eca6aac921328f8fb1f28c1068b9abbd8bb25cd..3956c8ce2a6794eb31ef5c269ffec1f620a2740e 100644 (file)
@@ -1,7 +1,5 @@
 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'
@@ -14,22 +12,7 @@ const startFastify = async (
     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
index 795a358bd52d85acedf2f3c87c4edd4e15d4626f..d995fd290f2977ad3bcee810da394ba53e49c91a 100644 (file)
@@ -8,13 +8,29 @@ const fastifyWorkerFile = join(
   `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(
index cdd67845060143ae31add0915abc671075ee6da6..e9c516f00a075cb2bfde3070cbd1ab909ce35fc5 100644 (file)
@@ -1,6 +1,6 @@
 import type { ThreadPoolOptions } from 'poolifier'
 
-export interface ClusterWorkerData {
+export interface ClusterWorkerData extends FastifyPoolifierOptions {
   port: number
 }
 
@@ -23,6 +23,6 @@ export interface ThreadWorkerResponse<T = unknown> {
 
 export interface FastifyPoolifierOptions extends ThreadPoolOptions {
   workerFile: string
-  maxWorkers?: number
   minWorkers?: number
+  maxWorkers?: number
 }
index 0bc9a0174d32823c313972d8e9a9876bc6bd0458..9e0cf696d4cc22492e947649599712203d4594e6 100644 (file)
@@ -14,6 +14,6 @@ export interface WorkerResponse<T = unknown> {
 
 export interface FastifyPoolifierOptions extends ThreadPoolOptions {
   workerFile: string
-  maxWorkers?: number
   minWorkers?: number
+  maxWorkers?: number
 }