refactor: add default options values to fastify example
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 11 Aug 2023 19:17:35 +0000 (21:17 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 11 Aug 2023 19:17:35 +0000 (21:17 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
examples/typescript/http-server-pool/fastify/src/fastify-poolifier.ts
examples/typescript/http-server-pool/fastify/src/types.ts

index 36c1ea623a06265a8b14e951653a4bfd08cbb456..8419e78194574f3372cf17089ade2c046135b0d4 100644 (file)
@@ -1,4 +1,4 @@
-import { DynamicThreadPool } from 'poolifier'
+import { DynamicThreadPool, availableParallelism } from 'poolifier'
 import { type FastifyPluginCallback } from 'fastify'
 import fp from 'fastify-plugin'
 import {
@@ -12,9 +12,16 @@ const fastifyPoolifierPlugin: FastifyPluginCallback<FastifyPoolifierOptions> = (
   options,
   done
 ) => {
+  options = {
+    ...{
+      minWorkers: 1,
+      maxWorkers: availableParallelism()
+    },
+    ...options
+  }
   const pool = new DynamicThreadPool<WorkerData, WorkerResponse>(
-    options.minWorkers,
-    options.maxWorkers,
+    options.minWorkers as number,
+    options.maxWorkers as number,
     options.workerFile,
     options
   )
index 5c53f1062551e6bd257513c4447629e2a3c544d7..0bc9a0174d32823c313972d8e9a9876bc6bd0458 100644 (file)
@@ -14,6 +14,6 @@ export interface WorkerResponse<T = unknown> {
 
 export interface FastifyPoolifierOptions extends ThreadPoolOptions {
   workerFile: string
-  maxWorkers: number
-  minWorkers: number
+  maxWorkers?: number
+  minWorkers?: number
 }