From 8538ea4ccb5672b3d4b846db1bedf80135c1df8b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 27 Sep 2023 20:11:02 +0200 Subject: [PATCH] refactor: refine code examples MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- README.md | 10 +++++----- examples/javascript/dynamicExample.js | 13 +++++++----- examples/javascript/fixedExample.js | 11 ++++++---- examples/javascript/yourWorker.js | 4 ---- .../express-cluster/src/worker.ts | 16 +++++++-------- .../src/request-handler-worker.ts | 20 +++++++++++-------- .../express-worker_threads/src/worker.ts | 20 +++++++++++-------- .../fastify-cluster/src/worker.ts | 16 +++++++-------- .../src/request-handler-worker.ts | 20 +++++++++++-------- .../fastify-worker_threads/src/worker.ts | 20 +++++++++++-------- .../ws-cluster/src/worker.ts | 18 +++++++++-------- .../ws-hybrid/src/request-handler-worker.ts | 20 +++++++++++-------- .../ws-worker_threads/src/worker.ts | 20 +++++++++++-------- 13 files changed, 118 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index affe8bc9..c0d1e26a 100644 --- a/README.md +++ b/README.md @@ -116,8 +116,8 @@ const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', { onlineHandler: () => console.info('worker is online') }) -pool.emitter.on(PoolEvents.ready, () => console.info('Pool is ready')) -pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy')) +pool.emitter?.on(PoolEvents.ready, () => console.info('Pool is ready')) +pool.emitter?.on(PoolEvents.busy, () => console.info('Pool is busy')) // or a dynamic worker_threads pool const pool = new DynamicThreadPool(Math.floor(availableParallelism() / 2), availableParallelism(), './yourWorker.js', { @@ -125,9 +125,9 @@ const pool = new DynamicThreadPool(Math.floor(availableParallelism() / 2), avail onlineHandler: () => console.info('worker is online') }) -pool.emitter.on(PoolEvents.full, () => console.info('Pool is full')) -pool.emitter.on(PoolEvents.ready, () => console.info('Pool is ready')) -pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy')) +pool.emitter?.on(PoolEvents.full, () => console.info('Pool is full')) +pool.emitter?.on(PoolEvents.ready, () => console.info('Pool is ready')) +pool.emitter?.on(PoolEvents.busy, () => console.info('Pool is busy')) // the execute method signature is the same for both implementations, // so you can easily switch from one to another diff --git a/examples/javascript/dynamicExample.js b/examples/javascript/dynamicExample.js index 33fdec97..2783c171 100644 --- a/examples/javascript/dynamicExample.js +++ b/examples/javascript/dynamicExample.js @@ -17,9 +17,9 @@ const pool = new DynamicThreadPool( let poolFull = 0 let poolReady = 0 let poolBusy = 0 -pool.emitter.on(PoolEvents.full, () => poolFull++) -pool.emitter.on(PoolEvents.ready, () => poolReady++) -pool.emitter.on(PoolEvents.busy, () => poolBusy++) +pool.emitter?.on(PoolEvents.full, () => poolFull++) +pool.emitter?.on(PoolEvents.ready, () => poolReady++) +pool.emitter?.on(PoolEvents.busy, () => poolBusy++) let resolved = 0 const start = performance.now() @@ -30,10 +30,13 @@ for (let i = 1; i <= iterations; i++) { .then(() => { resolved++ if (resolved === iterations) { - console.info(`Time taken is ${performance.now() - start}`) + console.info( + `Time taken is ${(performance.now() - start).toFixed(2)}ms` + ) console.info(`The pool was full for ${poolFull} times`) console.info(`The pool was ready for ${poolReady} times`) - return console.info(`The pool was busy for ${poolBusy} times`) + console.info(`The pool was busy for ${poolBusy} times`) + return pool.destroy() } return null }) diff --git a/examples/javascript/fixedExample.js b/examples/javascript/fixedExample.js index 66bfc8fc..8aab9cb7 100644 --- a/examples/javascript/fixedExample.js +++ b/examples/javascript/fixedExample.js @@ -11,8 +11,8 @@ const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', { }) let poolReady = 0 let poolBusy = 0 -pool.emitter.on(PoolEvents.ready, () => poolReady++) -pool.emitter.on(PoolEvents.busy, () => poolBusy++) +pool.emitter?.on(PoolEvents.ready, () => poolReady++) +pool.emitter?.on(PoolEvents.busy, () => poolBusy++) let resolved = 0 const start = performance.now() @@ -23,9 +23,12 @@ for (let i = 1; i <= iterations; i++) { .then(() => { resolved++ if (resolved === iterations) { - console.info(`Time taken is ${performance.now() - start}`) + console.info( + `Time taken is ${(performance.now() - start).toFixed(2)}ms` + ) console.info(`The pool was ready for ${poolReady} times`) - return console.info(`The pool was busy for ${poolBusy} times`) + console.info(`The pool was busy for ${poolBusy} times`) + return pool.destroy() } return null }) diff --git a/examples/javascript/yourWorker.js b/examples/javascript/yourWorker.js index cc462f7f..073253ea 100644 --- a/examples/javascript/yourWorker.js +++ b/examples/javascript/yourWorker.js @@ -1,9 +1,6 @@ 'use strict' -const { isMainThread } = require('node:worker_threads') const { ThreadWorker } = require('poolifier') -const debug = false - function yourFunction () { for (let i = 0; i <= 1000; i++) { const o = { @@ -11,7 +8,6 @@ function yourFunction () { } JSON.stringify(o) } - debug === true && console.info(`This is the main thread ${isMainThread}`) return { ok: 1 } } diff --git a/examples/typescript/http-server-pool/express-cluster/src/worker.ts b/examples/typescript/http-server-pool/express-cluster/src/worker.ts index 7dd607b5..940503df 100644 --- a/examples/typescript/http-server-pool/express-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/express-cluster/src/worker.ts @@ -4,16 +4,16 @@ import { ClusterWorker } from 'poolifier' import express, { type Express, type Request, type Response } from 'express' import type { WorkerData, WorkerResponse } from './types.js' -const factorial: (n: number) => number = n => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} - class ExpressWorker extends ClusterWorker { private static server: Server + private static readonly factorial = (n: number): number => { + if (n === 0) { + return 1 + } + return ExpressWorker.factorial(n - 1) * n + } + private static readonly startExpress = ( workerData?: WorkerData ): WorkerResponse => { @@ -30,7 +30,7 @@ class ExpressWorker extends ClusterWorker { application.get('/api/factorial/:number', (req: Request, res: Response) => { const { number } = req.params - res.send({ number: factorial(parseInt(number)) }).end() + res.send({ number: ExpressWorker.factorial(parseInt(number)) }).end() }) ExpressWorker.server = application.listen(port, () => { diff --git a/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts b/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts index 40cde50d..8c247819 100644 --- a/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts @@ -5,17 +5,17 @@ import { type ThreadWorkerResponse } from './types.js' -const factorial: (n: number) => number = n => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} - class RequestHandlerWorker< Data extends ThreadWorkerData, Response extends ThreadWorkerResponse > extends ThreadWorker { + private static readonly factorial = (n: number): number => { + if (n === 0) { + return 1 + } + return RequestHandlerWorker.factorial(n - 1) * n + } + public constructor () { super({ echo: (workerData?: Data) => { @@ -23,7 +23,11 @@ class RequestHandlerWorker< }, factorial: (workerData?: Data) => { return { - data: { number: factorial(workerData?.data?.number as number) } + data: { + number: RequestHandlerWorker.factorial( + workerData?.data?.number as number + ) + } } as unknown as Response } }) diff --git a/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts b/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts index 7589aa6b..3f573647 100644 --- a/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts +++ b/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts @@ -5,17 +5,17 @@ import { type WorkerResponse } from './types.js' -const factorial: (n: number) => number = n => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} - class RequestHandlerWorker< Data extends WorkerData, Response extends WorkerResponse > extends ThreadWorker { + private static readonly factorial: (n: number) => number = n => { + if (n === 0) { + return 1 + } + return RequestHandlerWorker.factorial(n - 1) * n + } + public constructor () { super({ echo: (workerData?: Data) => { @@ -23,7 +23,11 @@ class RequestHandlerWorker< }, factorial: (workerData?: Data) => { return { - body: { number: factorial(workerData?.body?.number as number) } + body: { + number: RequestHandlerWorker.factorial( + workerData?.body?.number as number + ) + } } as unknown as Response } }) diff --git a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts index ee64ee2e..2f6c8f6d 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -3,16 +3,16 @@ import { ClusterWorker } from 'poolifier' import Fastify, { type FastifyInstance } from 'fastify' import type { WorkerData, WorkerResponse } from './types.js' -const factorial: (n: number) => number = n => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} - class FastifyWorker extends ClusterWorker { private static fastify: FastifyInstance + private static readonly factorial = (n: number): number => { + if (n === 0) { + return 1 + } + return FastifyWorker.factorial(n - 1) * n + } + private static readonly startFastify = async ( workerData?: WorkerData ): Promise => { @@ -30,7 +30,7 @@ class FastifyWorker extends ClusterWorker { Params: { number: number } }>('/api/factorial/:number', request => { const { number } = request.params - return { number: factorial(number) } + return { number: FastifyWorker.factorial(number) } }) await FastifyWorker.fastify.listen({ port }) diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts index 40cde50d..8c247819 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts @@ -5,17 +5,17 @@ import { type ThreadWorkerResponse } from './types.js' -const factorial: (n: number) => number = n => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} - class RequestHandlerWorker< Data extends ThreadWorkerData, Response extends ThreadWorkerResponse > extends ThreadWorker { + private static readonly factorial = (n: number): number => { + if (n === 0) { + return 1 + } + return RequestHandlerWorker.factorial(n - 1) * n + } + public constructor () { super({ echo: (workerData?: Data) => { @@ -23,7 +23,11 @@ class RequestHandlerWorker< }, factorial: (workerData?: Data) => { return { - data: { number: factorial(workerData?.data?.number as number) } + data: { + number: RequestHandlerWorker.factorial( + workerData?.data?.number as number + ) + } } as unknown as Response } }) diff --git a/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts b/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts index 7589aa6b..3f573647 100644 --- a/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts @@ -5,17 +5,17 @@ import { type WorkerResponse } from './types.js' -const factorial: (n: number) => number = n => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} - class RequestHandlerWorker< Data extends WorkerData, Response extends WorkerResponse > extends ThreadWorker { + private static readonly factorial: (n: number) => number = n => { + if (n === 0) { + return 1 + } + return RequestHandlerWorker.factorial(n - 1) * n + } + public constructor () { super({ echo: (workerData?: Data) => { @@ -23,7 +23,11 @@ class RequestHandlerWorker< }, factorial: (workerData?: Data) => { return { - body: { number: factorial(workerData?.body?.number as number) } + body: { + number: RequestHandlerWorker.factorial( + workerData?.body?.number as number + ) + } } as unknown as Response } }) diff --git a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts index 5e758eb7..0e8dc20a 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts @@ -8,16 +8,16 @@ import { type WorkerResponse } from './types.js' -const factorial: (n: number) => number = n => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} - class WebSocketServerWorker extends ClusterWorker { private static wss: WebSocketServer + private static readonly factorial = (n: number): number => { + if (n === 0) { + return 1 + } + return WebSocketServerWorker.factorial(n - 1) * n + } + private static readonly startWebSocketServer = ( workerData?: WorkerData ): WorkerResponse => { @@ -49,7 +49,9 @@ class WebSocketServerWorker extends ClusterWorker { ws.send( JSON.stringify({ type: MessageType.factorial, - data: { number: factorial(data.number as number) } + data: { + number: WebSocketServerWorker.factorial(data.number as number) + } }) ) break diff --git a/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts b/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts index 40cde50d..8c247819 100644 --- a/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts @@ -5,17 +5,17 @@ import { type ThreadWorkerResponse } from './types.js' -const factorial: (n: number) => number = n => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} - class RequestHandlerWorker< Data extends ThreadWorkerData, Response extends ThreadWorkerResponse > extends ThreadWorker { + private static readonly factorial = (n: number): number => { + if (n === 0) { + return 1 + } + return RequestHandlerWorker.factorial(n - 1) * n + } + public constructor () { super({ echo: (workerData?: Data) => { @@ -23,7 +23,11 @@ class RequestHandlerWorker< }, factorial: (workerData?: Data) => { return { - data: { number: factorial(workerData?.data?.number as number) } + data: { + number: RequestHandlerWorker.factorial( + workerData?.data?.number as number + ) + } } as unknown as Response } }) diff --git a/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts b/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts index 92b9d64a..6f9f0914 100644 --- a/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts +++ b/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts @@ -5,17 +5,17 @@ import { type WorkerResponse } from './types.js' -const factorial: (n: number) => number = n => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} - class RequestHandlerWorker< Data extends WorkerData, Response extends WorkerResponse > extends ThreadWorker { + private static readonly factorial = (n: number): number => { + if (n === 0) { + return 1 + } + return RequestHandlerWorker.factorial(n - 1) * n + } + public constructor () { super({ echo: (workerData?: Data) => { @@ -23,7 +23,11 @@ class RequestHandlerWorker< }, factorial: (workerData?: Data) => { return { - data: { number: factorial(workerData?.data?.number as number) } + data: { + number: RequestHandlerWorker.factorial( + workerData?.data?.number as number + ) + } } as unknown as Response } }) -- 2.34.1