From 503bda5baced9515d2fc20b85d4deb3da2dab93a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 13 Aug 2023 18:10:13 +0200 Subject: [PATCH] refactor: move worker_threads pool init in main method in fastify-hybrid example MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../fastify-hybrid/src/fastify-worker.ts | 21 ++----------------- .../fastify-hybrid/src/main.ts | 18 +++++++++++++++- .../fastify-hybrid/src/types.ts | 4 ++-- .../fastify-worker_threads/src/types.ts | 2 +- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts index 1eca6aac..3956c8ce 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts @@ -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 diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/main.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/main.ts index 795a358b..d995fd29 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/main.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/main.ts @@ -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( 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( diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/types.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/types.ts index cdd67845..e9c516f0 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/types.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/types.ts @@ -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 { export interface FastifyPoolifierOptions extends ThreadPoolOptions { workerFile: string - maxWorkers?: number minWorkers?: number + maxWorkers?: number } diff --git a/examples/typescript/http-server-pool/fastify-worker_threads/src/types.ts b/examples/typescript/http-server-pool/fastify-worker_threads/src/types.ts index 0bc9a017..9e0cf696 100644 --- a/examples/typescript/http-server-pool/fastify-worker_threads/src/types.ts +++ b/examples/typescript/http-server-pool/fastify-worker_threads/src/types.ts @@ -14,6 +14,6 @@ export interface WorkerResponse { export interface FastifyPoolifierOptions extends ThreadPoolOptions { workerFile: string - maxWorkers?: number minWorkers?: number + maxWorkers?: number } -- 2.34.1