From: Jérôme Benoit Date: Sun, 13 Aug 2023 18:00:04 +0000 (+0200) Subject: refactor: encapsulate code into classes in examples X-Git-Tag: v2.6.25~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=3d49c6d2f943532974cefa71f13af2c313555d95;p=poolifier.git refactor: encapsulate code into classes in examples Signed-off-by: Jérôme Benoit --- 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 f679b4a6..4084bf71 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -1,6 +1,6 @@ import type { AddressInfo } from 'node:net' import { ClusterWorker } from 'poolifier' -import Fastify from 'fastify' +import Fastify, { type FastifyInstance } from 'fastify' import type { WorkerData, WorkerResponse } from './types.js' const factorial: (n: number) => number = n => { @@ -10,35 +10,37 @@ const factorial: (n: number) => number = n => { return factorial(n - 1) * n } -const startFastify = async ( - workerData?: WorkerData -): Promise => { - const { port } = workerData as WorkerData - const fastify = Fastify({ - logger: true - }) - - fastify.all('/api/echo', request => { - return request.body - }) - - fastify.get<{ - Params: { number: number } - }>('/api/factorial/:number', request => { - const { number } = request.params - return { number: factorial(number) } - }) - - await fastify.listen({ port }) - return { - status: true, - port: (fastify.server.address() as AddressInfo).port +class FastifyWorker extends ClusterWorker { + private static fastify: FastifyInstance + + private static readonly startFastify = async ( + workerData?: WorkerData + ): Promise => { + const { port } = workerData as WorkerData + FastifyWorker.fastify = Fastify({ + logger: true + }) + + FastifyWorker.fastify.all('/api/echo', request => { + return request.body + }) + + FastifyWorker.fastify.get<{ + Params: { number: number } + }>('/api/factorial/:number', request => { + const { number } = request.params + return { number: factorial(number) } + }) + + await FastifyWorker.fastify.listen({ port }) + return { + status: true, + port: (FastifyWorker.fastify.server.address() as AddressInfo).port + } } -} -class FastifyWorker extends ClusterWorker { public constructor () { - super(startFastify) + super(FastifyWorker.startFastify) } } 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 3956c8ce..dd77f123 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,43 +1,49 @@ import type { AddressInfo } from 'node:net' import { ClusterWorker } from 'poolifier' -import Fastify from 'fastify' +import Fastify, { type FastifyInstance } from 'fastify' import type { ClusterWorkerData, ClusterWorkerResponse } from './types.js' import { fastifyPoolifier } from './fastify-poolifier.js' -const startFastify = async ( - workerData?: ClusterWorkerData -): Promise => { - const { port } = workerData as ClusterWorkerData - const fastify = Fastify({ - logger: true - }) - - await fastify.register(fastifyPoolifier, workerData) - - fastify.all('/api/echo', async request => { - return (await fastify.execute({ body: request.body }, 'echo')).body - }) - - fastify.get<{ - Params: { number: number } - }>('/api/factorial/:number', async request => { - const { number } = request.params - return (await fastify.execute({ body: { number } }, 'factorial')).body - }) - - await fastify.listen({ port }) - return { - status: true, - port: (fastify.server.address() as AddressInfo).port - } -} - class FastifyWorker extends ClusterWorker< ClusterWorkerData, ClusterWorkerResponse > { + private static fastify: FastifyInstance + + private static readonly startFastify = async ( + workerData?: ClusterWorkerData + ): Promise => { + const { port } = workerData as ClusterWorkerData + FastifyWorker.fastify = Fastify({ + logger: true + }) + + await FastifyWorker.fastify.register(fastifyPoolifier, workerData) + + FastifyWorker.fastify.all('/api/echo', async request => { + return ( + await FastifyWorker.fastify.execute({ body: request.body }, 'echo') + ).body + }) + + FastifyWorker.fastify.get<{ + Params: { number: number } + }>('/api/factorial/:number', async request => { + const { number } = request.params + return ( + await FastifyWorker.fastify.execute({ body: { number } }, 'factorial') + ).body + }) + + await FastifyWorker.fastify.listen({ port }) + return { + status: true, + port: (FastifyWorker.fastify.server.address() as AddressInfo).port + } + } + public constructor () { - super(startFastify) + super(FastifyWorker.startFastify) } } 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 f261a6d7..c82c7e63 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts @@ -15,50 +15,54 @@ const factorial: (n: number) => number = n => { return factorial(n - 1) * n } -const startWebSocketServer = (workerData?: WorkerData): WorkerResponse => { - const { port } = workerData as WorkerData - const wss = new WebSocketServer({ port }, () => { - console.info( - `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/` - ) - }) +class WebSocketServerWorker extends ClusterWorker { + private static wss: WebSocketServer + + private static readonly startWebSocketServer = ( + workerData?: WorkerData + ): WorkerResponse => { + const { port } = workerData as WorkerData + WebSocketServerWorker.wss = new WebSocketServer({ port }, () => { + console.info( + `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/` + ) + }) - wss.on('connection', ws => { - ws.on('error', console.error) - ws.on('message', (message: RawData) => { - const { type, data } = JSON.parse( - // eslint-disable-next-line @typescript-eslint/no-base-to-string - message.toString() - ) as MessagePayload - switch (type) { - case MessageType.echo: - ws.send( - JSON.stringify({ - type: MessageType.echo, - data - }) - ) - break - case MessageType.factorial: - ws.send( - JSON.stringify({ - type: MessageType.factorial, - data: { number: factorial(data.number as number) } - }) - ) - break - } + WebSocketServerWorker.wss.on('connection', ws => { + ws.on('error', console.error) + ws.on('message', (message: RawData) => { + const { type, data } = JSON.parse( + // eslint-disable-next-line @typescript-eslint/no-base-to-string + message.toString() + ) as MessagePayload + switch (type) { + case MessageType.echo: + ws.send( + JSON.stringify({ + type: MessageType.echo, + data + }) + ) + break + case MessageType.factorial: + ws.send( + JSON.stringify({ + type: MessageType.factorial, + data: { number: factorial(data.number as number) } + }) + ) + break + } + }) }) - }) - return { - status: true, - port: wss.options.port + return { + status: true, + port: WebSocketServerWorker.wss.options.port + } } -} -class WebSocketServerWorker extends ClusterWorker { public constructor () { - super(startWebSocketServer) + super(WebSocketServerWorker.startWebSocketServer) } } diff --git a/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts b/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts index 8b1bf2e8..bd7b8fbb 100644 --- a/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts +++ b/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts @@ -22,11 +22,17 @@ class WebSocketServerWorker extends ClusterWorker< ClusterWorkerData, ClusterWorkerResponse > { + private static wss: WebSocketServer + private static requestHandlerPool: DynamicThreadPool< + ThreadWorkerData, + ThreadWorkerResponse + > + private static readonly startWebSocketServer = ( workerData?: ClusterWorkerData ): ClusterWorkerResponse => { const { port } = workerData as ClusterWorkerData - const wss = new WebSocketServer({ port }, () => { + WebSocketServerWorker.wss = new WebSocketServer({ port }, () => { console.info( `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/` ) @@ -41,7 +47,7 @@ ClusterWorkerResponse workerData?.workerFile as string ) - wss.on('connection', ws => { + WebSocketServerWorker.wss.on('connection', ws => { ws.on('error', console.error) ws.on('message', (message: RawData) => { const { type, data } = JSON.parse( @@ -82,15 +88,10 @@ ClusterWorkerResponse }) return { status: true, - port: wss.options.port + port: WebSocketServerWorker.wss.options.port } } - private static requestHandlerPool: DynamicThreadPool< - ThreadWorkerData, - ThreadWorkerResponse - > - public constructor () { super(WebSocketServerWorker.startWebSocketServer) }