refactor: move pools options handling in main ws-hybrid example
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 13 Aug 2023 17:36:52 +0000 (19:36 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 13 Aug 2023 17:36:52 +0000 (19:36 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
examples/typescript/websocket-server-pool/ws-hybrid/src/main.ts
examples/typescript/websocket-server-pool/ws-hybrid/src/types.ts
examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts
package.json
pnpm-lock.yaml

index 6f73fa0731504404cf5d4b26d7afdc5fc121be9d..d9e827cbfa262f83613cb01c8201b70efdfec4a7 100644 (file)
@@ -8,13 +8,29 @@ const webSocketServerWorkerFile = join(
   `websocket-server-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),
   webSocketServerWorkerFile,
   {
     onlineHandler: () => {
       pool
-        .execute({ port: 8080 })
+        .execute({
+          port: 8080,
+          maxWorkers: Math.round(availableParallelism() / 2),
+          workerFile: requestHandlerWorkerFile,
+          enableTasksQueue: true,
+          tasksQueueOptions: {
+            concurrency: 8
+          },
+          errorHandler: (e: Error) => {
+            console.error('Thread worker error:', e)
+          }
+        })
         .then(response => {
           if (response.status) {
             console.info(
index 055b0456e70e8215b3af292771a8c1044890fc53..d52dbf2396569595aae7918775f208bdcd54154f 100644 (file)
@@ -1,3 +1,5 @@
+import type { ThreadPoolOptions } from 'poolifier'
+
 export enum MessageType {
   echo = 'echo',
   factorial = 'factorial'
@@ -12,8 +14,11 @@ export interface DataPayload {
   number?: number
 }
 
-export interface ClusterWorkerData {
+export interface ClusterWorkerData extends ThreadPoolOptions {
   port: number
+  workerFile: string
+  minWorkers?: number
+  maxWorkers?: number
 }
 
 export interface ClusterWorkerResponse {
index 2418be61d0487e9ce43bda018e41a4b8f5117579..8b1bf2e83ca25dba3c590be6817c241e4e6f4ce4 100644 (file)
@@ -1,5 +1,3 @@
-import { dirname, extname, join } from 'node:path'
-import { fileURLToPath } from 'node:url'
 import {
   ClusterWorker,
   DynamicThreadPool,
@@ -34,6 +32,15 @@ ClusterWorkerResponse
       )
     })
 
+    WebSocketServerWorker.requestHandlerPool = new DynamicThreadPool<
+    ThreadWorkerData<DataPayload>,
+    ThreadWorkerResponse<DataPayload>
+    >(
+      workerData?.minWorkers ?? 1,
+      workerData?.maxWorkers ?? availableParallelism(),
+      workerData?.workerFile as string
+    )
+
     wss.on('connection', ws => {
       ws.on('error', console.error)
       ws.on('message', (message: RawData) => {
@@ -43,7 +50,7 @@ ClusterWorkerResponse
         ) as MessagePayload<DataPayload>
         switch (type) {
           case MessageType.echo:
-            this.requestHandlerPool
+            WebSocketServerWorker.requestHandlerPool
               .execute({ data }, 'echo')
               .then(response => {
                 ws.send(
@@ -57,7 +64,7 @@ ClusterWorkerResponse
               .catch(emptyFunction)
             break
           case MessageType.factorial:
-            this.requestHandlerPool
+            WebSocketServerWorker.requestHandlerPool
               .execute({ data }, 'factorial')
               .then(response => {
                 ws.send(
@@ -79,28 +86,10 @@ ClusterWorkerResponse
     }
   }
 
-  private static readonly requestHandlerWorkerFile = join(
-    dirname(fileURLToPath(import.meta.url)),
-    `request-handler-worker${extname(fileURLToPath(import.meta.url))}`
-  )
-
-  private static readonly requestHandlerPool = new DynamicThreadPool<
+  private static requestHandlerPool: DynamicThreadPool<
   ThreadWorkerData<DataPayload>,
   ThreadWorkerResponse<DataPayload>
-  >(
-    1,
-    Math.round(availableParallelism() / 2),
-    WebSocketServerWorker.requestHandlerWorkerFile,
-    {
-      enableTasksQueue: true,
-      tasksQueueOptions: {
-        concurrency: 8
-      },
-      errorHandler: (e: Error) => {
-        console.error('Thread worker error:', e)
-      }
-    }
-  )
+  >
 
   public constructor () {
     super(WebSocketServerWorker.startWebSocketServer)
index 495720cb419b324c540a22a91725ac30fd53726e..a854642a0b5d8cc76d40939ff06baef695a3d357 100644 (file)
     "eslint-plugin-tsdoc": "^0.2.17",
     "expect": "^29.6.2",
     "husky": "^8.0.3",
-    "lint-staged": "^13.2.3",
+    "lint-staged": "^13.3.0",
     "microtime": "^3.1.1",
     "mocha": "^10.2.0",
     "mochawesome": "^7.1.3",
     "prettier": "^3.0.1",
-    "release-it": "^16.1.4",
+    "release-it": "^16.1.5",
     "rollup": "^3.28.0",
     "rollup-plugin-analyzer": "^4.0.0",
     "rollup-plugin-command": "^1.1.3",
index b6853f1e0068a2ba5adaa225db61a70a93c23cff..977729d6aedac6670360076faded331270b80e59 100644 (file)
@@ -16,10 +16,10 @@ devDependencies:
     version: 17.7.0
   '@release-it/bumper':
     specifier: ^5.1.0
-    version: 5.1.0(release-it@16.1.4)
+    version: 5.1.0(release-it@16.1.5)
   '@release-it/keep-a-changelog':
     specifier: ^4.0.0
-    version: 4.0.0(release-it@16.1.4)
+    version: 4.0.0(release-it@16.1.5)
   '@rollup/plugin-terser':
     specifier: ^0.4.3
     version: 0.4.3(rollup@3.28.0)
@@ -81,8 +81,8 @@ devDependencies:
     specifier: ^8.0.3
     version: 8.0.3
   lint-staged:
-    specifier: ^13.2.3
-    version: 13.2.3
+    specifier: ^13.3.0
+    version: 13.3.0
   microtime:
     specifier: ^3.1.1
     version: 3.1.1
@@ -96,8 +96,8 @@ devDependencies:
     specifier: ^3.0.1
     version: 3.0.1
   release-it:
-    specifier: ^16.1.4
-    version: 16.1.4
+    specifier: ^16.1.5
+    version: 16.1.5
   rollup:
     specifier: ^3.28.0
     version: 3.28.0
@@ -692,7 +692,7 @@ packages:
       config-chain: 1.1.13
     dev: true
 
-  /@release-it/bumper@5.1.0(release-it@16.1.4):
+  /@release-it/bumper@5.1.0(release-it@16.1.5):
     resolution: {integrity: sha512-mnM5QQJvK+4sqLpU7vkDbGMGh1QNwdnWNPqrkfOCyr/cKgRDvb8e+nbiNZzyI/1XsxSjnfLHDecRVuqucwE6BQ==}
     engines: {node: '>=16'}
     peerDependencies:
@@ -704,18 +704,18 @@ packages:
       ini: 4.1.1
       js-yaml: 4.1.0
       lodash-es: 4.17.21
-      release-it: 16.1.4
+      release-it: 16.1.5
       semver: 7.5.4
     dev: true
 
-  /@release-it/keep-a-changelog@4.0.0(release-it@16.1.4):
+  /@release-it/keep-a-changelog@4.0.0(release-it@16.1.5):
     resolution: {integrity: sha512-a3n6RkBaYGf2qK2Yz/FkHDkpJjLYtMvpoZBIGfOz1zmv+918YU3In9zjoBJn4f2PldIW5yTWuk81hMYAdgPFww==}
     engines: {node: '>=16'}
     peerDependencies:
       release-it: ^16.0.0
     dependencies:
       detect-newline: 4.0.0
-      release-it: 16.1.4
+      release-it: 16.1.5
       string-template: 1.0.0
     dev: true
 
@@ -747,14 +747,14 @@ packages:
       tslib:
         optional: true
     dependencies:
-      '@rollup/pluginutils': 5.0.2(rollup@3.28.0)
+      '@rollup/pluginutils': 5.0.3(rollup@3.28.0)
       resolve: 1.22.4
       rollup: 3.28.0
       typescript: 5.1.6
     dev: true
 
-  /@rollup/pluginutils@5.0.2(rollup@3.28.0):
-    resolution: {integrity: sha512-pTd9rIsP92h+B6wWwFbW8RkZv4hiR/xKsqre4SIuAOaOEQRxi0lqLke9k2/7WegC85GgUs9pjmOjCUi3In4vwA==}
+  /@rollup/pluginutils@5.0.3(rollup@3.28.0):
+    resolution: {integrity: sha512-hfllNN4a80rwNQ9QCxhxuHCGHMAvabXqxNdaChUSSadMre7t4iEUI6fFAhBOn/eIYTgYVhBv7vCLsAJ4u3lf3g==}
     engines: {node: '>=14.0.0'}
     peerDependencies:
       rollup: ^1.20.0||^2.0.0||^3.0.0
@@ -1121,6 +1121,13 @@ packages:
       type-fest: 0.21.3
     dev: true
 
+  /ansi-escapes@5.0.0:
+    resolution: {integrity: sha512-5GFMVX8HqE/TB+FuBJGuO5XG0WrsA6ptUqoODaT/n9mmUaZFkqnBueB4leqGBCmrUHnCnC4PCZTCd0E7QQ83bA==}
+    engines: {node: '>=12'}
+    dependencies:
+      type-fest: 1.4.0
+    dev: true
+
   /ansi-regex@5.0.1:
     resolution: {integrity: sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==}
     engines: {node: '>=8'}
@@ -1535,11 +1542,6 @@ packages:
       supports-color: 7.2.0
     dev: true
 
-  /chalk@5.2.0:
-    resolution: {integrity: sha512-ree3Gqw/nazQAPuJJEy+avdl7QfZMcUvmHIKgEZkGL+xOBzRvup5Hxo6LHuMceSxOabuJLJm5Yp/92R9eMmMvA==}
-    engines: {node: ^12.17.0 || ^14.13 || >=16.0.0}
-    dev: true
-
   /chalk@5.3.0:
     resolution: {integrity: sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==}
     engines: {node: ^12.17.0 || ^14.13 || >=16.0.0}
@@ -1598,14 +1600,6 @@ packages:
     engines: {node: '>=6'}
     dev: true
 
-  /cli-truncate@2.1.0:
-    resolution: {integrity: sha512-n8fOixwDD6b/ObinzTrp1ZKFzbgvKZvuz/TvejnLn1aQfC6r52XEx85FmuC+3HI+JM7coBRXUvNqEU2PHVrHpg==}
-    engines: {node: '>=8'}
-    dependencies:
-      slice-ansi: 3.0.0
-      string-width: 4.2.3
-    dev: true
-
   /cli-truncate@3.1.0:
     resolution: {integrity: sha512-wfOBkjXteqSnI59oPcJkcPl/ZmwvMMOj340qUIY1SKZCv0B9Cf4D4fAucRkIKQmsIuYK3x1rrgU7MeGRruiuiA==}
     engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
@@ -1666,9 +1660,9 @@ packages:
     resolution: {integrity: sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==}
     dev: true
 
-  /commander@10.0.1:
-    resolution: {integrity: sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==}
-    engines: {node: '>=14'}
+  /commander@11.0.0:
+    resolution: {integrity: sha512-9HMlXtt/BNoYr8ooyjjNRdIilOTkVJXB+GhxMTtOKwk0R4j4lS4NpjuqmRxroBfnfTSHQIHQB7wryHhXarNjmQ==}
+    engines: {node: '>=16'}
     dev: true
 
   /commander@2.20.3:
@@ -2623,6 +2617,10 @@ packages:
     engines: {node: '>=0.10.0'}
     dev: true
 
+  /eventemitter3@5.0.1:
+    resolution: {integrity: sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==}
+    dev: true
+
   /execa@5.1.1:
     resolution: {integrity: sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==}
     engines: {node: '>=10'}
@@ -3864,21 +3862,18 @@ packages:
     resolution: {integrity: sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==}
     dev: true
 
-  /lint-staged@13.2.3:
-    resolution: {integrity: sha512-zVVEXLuQIhr1Y7R7YAWx4TZLdvuzk7DnmrsTNL0fax6Z3jrpFcas+vKbzxhhvp6TA55m1SQuWkpzI1qbfDZbAg==}
-    engines: {node: ^14.13.1 || >=16.0.0}
+  /lint-staged@13.3.0:
+    resolution: {integrity: sha512-mPRtrYnipYYv1FEE134ufbWpeggNTo+O/UPzngoaKzbzHAthvR55am+8GfHTnqNRQVRRrYQLGW9ZyUoD7DsBHQ==}
+    engines: {node: ^16.14.0 || >=18.0.0}
     hasBin: true
     dependencies:
-      chalk: 5.2.0
-      cli-truncate: 3.1.0
-      commander: 10.0.1
+      chalk: 5.3.0
+      commander: 11.0.0
       debug: 4.3.4(supports-color@8.1.1)
       execa: 7.2.0
       lilconfig: 2.1.0
-      listr2: 5.0.8
+      listr2: 6.6.1
       micromatch: 4.0.5
-      normalize-path: 3.0.0
-      object-inspect: 1.12.3
       pidtree: 0.6.0
       string-argv: 0.3.2
       yaml: 2.3.1
@@ -3887,23 +3882,21 @@ packages:
       - supports-color
     dev: true
 
-  /listr2@5.0.8:
-    resolution: {integrity: sha512-mC73LitKHj9w6v30nLNGPetZIlfpUniNSsxxrbaPcWOjDb92SHPzJPi/t+v1YC/lxKz/AJ9egOjww0qUuFxBpA==}
-    engines: {node: ^14.13.1 || >=16.0.0}
+  /listr2@6.6.1:
+    resolution: {integrity: sha512-+rAXGHh0fkEWdXBmX+L6mmfmXmXvDGEKzkjxO+8mP3+nI/r/CWznVBvsibXdxda9Zz0OW2e2ikphN3OwCT/jSg==}
+    engines: {node: '>=16.0.0'}
     peerDependencies:
       enquirer: '>= 2.3.0 < 3'
     peerDependenciesMeta:
       enquirer:
         optional: true
     dependencies:
-      cli-truncate: 2.1.0
+      cli-truncate: 3.1.0
       colorette: 2.0.20
-      log-update: 4.0.0
-      p-map: 4.0.0
+      eventemitter3: 5.0.1
+      log-update: 5.0.1
       rfdc: 1.3.0
-      rxjs: 7.8.1
-      through: 2.3.8
-      wrap-ansi: 7.0.0
+      wrap-ansi: 8.1.0
     dev: true
 
   /load-json-file@5.3.0:
@@ -4053,6 +4046,17 @@ packages:
       wrap-ansi: 6.2.0
     dev: true
 
+  /log-update@5.0.1:
+    resolution: {integrity: sha512-5UtUDQ/6edw4ofyljDNcOVJQ4c7OjDro4h3y8e1GQL5iYElYclVHJ3zeWchylvMaKnDbDilC8irOVyexnA/Slw==}
+    engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
+    dependencies:
+      ansi-escapes: 5.0.0
+      cli-cursor: 4.0.0
+      slice-ansi: 5.0.0
+      strip-ansi: 7.1.0
+      wrap-ansi: 8.1.0
+    dev: true
+
   /loose-envify@1.4.0:
     resolution: {integrity: sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==}
     hasBin: true
@@ -4635,13 +4639,6 @@ packages:
       aggregate-error: 3.1.0
     dev: true
 
-  /p-map@4.0.0:
-    resolution: {integrity: sha512-/bjOqmgETBYB5BoEeGVea8dmvHb2m9GLy1E9W43yeyfP6QQCZGFNa+XRceJEuDB6zqr+gKpIAmlLebMpykw/MQ==}
-    engines: {node: '>=10'}
-    dependencies:
-      aggregate-error: 3.1.0
-    dev: true
-
   /p-try@2.2.0:
     resolution: {integrity: sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==}
     engines: {node: '>=6'}
@@ -4996,8 +4993,8 @@ packages:
       rc: 1.2.8
     dev: true
 
-  /release-it@16.1.4:
-    resolution: {integrity: sha512-lLYuXfZzAzO9tDyhQKpvkrUMY43/+PMTzibcUsC4dDDzDf3JT69FhiAjkqXv9nBAotyENGMoeHtK0jRiL4YeEQ==}
+  /release-it@16.1.5:
+    resolution: {integrity: sha512-w/zCljPZBSYcCwR9fjDB1zaYwie1CAQganUrwNqjtXacXhrrsS5E6dDUNLcxm2ypu8GWAgZNMJfuBJqIO2E7fA==}
     engines: {node: '>=16'}
     hasBin: true
     dependencies:
@@ -5302,15 +5299,6 @@ packages:
     engines: {node: '>=12'}
     dev: true
 
-  /slice-ansi@3.0.0:
-    resolution: {integrity: sha512-pSyv7bSTC7ig9Dcgbw9AuRNUb5k5V6oDudjZoMBSr13qpLBG7tB+zgCkARjq7xIUgdz5P1Qe8u+rSGdouOOIyQ==}
-    engines: {node: '>=8'}
-    dependencies:
-      ansi-styles: 4.3.0
-      astral-regex: 2.0.0
-      is-fullwidth-code-point: 3.0.0
-    dev: true
-
   /slice-ansi@4.0.0:
     resolution: {integrity: sha512-qMCMfhY040cVHT43K9BFygqYbUPFZKHOg7K73mtTWJRb8pyP3fzf4Ixd5SzdEJQ6MRUg/WBnOLxghZtKKurENQ==}
     engines: {node: '>=10'}