X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fhttp-server-pool%2Ffastify-cluster%2Fsrc%2Fworker.ts;h=e491fa8f4b05c3ee4712740e1e669a102775108e;hb=e443442b52eadc47d77f906586286f060ebff5ad;hp=283a223e3438aa07af36343368d6dd03de05017e;hpb=1fa0cdf5c1471f14c09c7a33131f14f13215996b;p=poolifier.git 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 283a223e..e491fa8f 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -1,42 +1,50 @@ import type { AddressInfo } from 'node:net' -import { ClusterWorker } from 'poolifier' + import Fastify, { type FastifyInstance } from 'fastify' -import type { WorkerData, WorkerResponse } from './types.js' +import { ClusterWorker } from 'poolifier' -const factorial: (n: number) => number = (n) => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} +import type { WorkerData, WorkerResponse } from './types.js' class FastifyWorker extends ClusterWorker { private static fastify: FastifyInstance + private static readonly factorial = (n: number | bigint): bigint => { + if (n === 0 || n === 1) { + return 1n + } else { + n = BigInt(n) + let factorial = 1n + for (let i = 1n; i <= n; i++) { + factorial *= i + } + return factorial + } + } + private static readonly startFastify = async ( workerData?: WorkerData ): Promise => { - const { port } = workerData as WorkerData + const { port } = workerData! FastifyWorker.fastify = Fastify({ logger: true }) - FastifyWorker.fastify.all('/api/echo', (request) => { + FastifyWorker.fastify.all('/api/echo', request => { return request.body }) FastifyWorker.fastify.get<{ Params: { number: number } - }>('/api/factorial/:number', (request) => { + }>('/api/factorial/:number', request => { const { number } = request.params - return { number: factorial(number) } + return { number: FastifyWorker.factorial(number).toString() } }) await FastifyWorker.fastify.listen({ port }) return { status: true, - port: (FastifyWorker.fastify.server.address() as AddressInfo)?.port + port: (FastifyWorker.fastify.server.address() as AddressInfo).port } }