From: Jérôme Benoit Date: Fri, 11 Aug 2023 17:35:18 +0000 (+0200) Subject: feat: add multithreaded factorial endpoint to express example X-Git-Tag: v2.6.23~4 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=167c661ce07656c302c6997328dde39402a44c56;p=poolifier.git feat: add multithreaded factorial endpoint to express example Signed-off-by: Jérôme Benoit --- diff --git a/.eslintrc.js b/.eslintrc.js index 2b402eac..44b9c5d2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -73,6 +73,7 @@ module.exports = defineConfig({ 'poolifier', 'poolify', 'readonly', + 'req', 'resize', 'sinon', 'threadjs', diff --git a/examples/typescript/http-server-pool/express/requests.sh b/examples/typescript/http-server-pool/express/requests.sh index e5ad31b3..6f7fe0ea 100755 --- a/examples/typescript/http-server-pool/express/requests.sh +++ b/examples/typescript/http-server-pool/express/requests.sh @@ -6,3 +6,8 @@ for ((request=1;request<=60;request++)) do time curl -i -H "Content-Type: application/json" -X POST -d '{"key1":"value1", "key2":"value2"}' http://localhost:8080/api/echo done + +for ((request=1;request<=60;request++)) +do + time curl -i http://localhost:8080/api/factorial/30 +done diff --git a/examples/typescript/http-server-pool/express/src/main.ts b/examples/typescript/http-server-pool/express/src/main.ts index 02e13939..b7a713e0 100644 --- a/examples/typescript/http-server-pool/express/src/main.ts +++ b/examples/typescript/http-server-pool/express/src/main.ts @@ -27,6 +27,16 @@ expressApp.all('/api/echo', (req: Request, res: Response) => { .catch(emptyFunction) }) +expressApp.get('/api/factorial/:number', (req: Request, res: Response) => { + const { number } = req.params + requestHandlerPool + .execute({ body: { number } }, 'factorial') + .then(response => { + return res.send(response.body).end() + }) + .catch(emptyFunction) +}) + try { expressApp.listen(port, () => { console.info( diff --git a/examples/typescript/http-server-pool/express/src/types.ts b/examples/typescript/http-server-pool/express/src/types.ts index 585e8723..3bd29215 100644 --- a/examples/typescript/http-server-pool/express/src/types.ts +++ b/examples/typescript/http-server-pool/express/src/types.ts @@ -1,3 +1,7 @@ +export interface BodyPayload { + number?: number +} + export interface WorkerData { body: T } diff --git a/examples/typescript/http-server-pool/express/src/worker.ts b/examples/typescript/http-server-pool/express/src/worker.ts index f006ad48..7589aa6b 100644 --- a/examples/typescript/http-server-pool/express/src/worker.ts +++ b/examples/typescript/http-server-pool/express/src/worker.ts @@ -1,20 +1,36 @@ import { ThreadWorker } from 'poolifier' -import { type WorkerData, type WorkerResponse } from './types.js' +import { + type BodyPayload, + type WorkerData, + 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 + Data extends WorkerData, + Response extends WorkerResponse > extends ThreadWorker { public constructor () { super({ echo: (workerData?: Data) => { return workerData as unknown as Response + }, + factorial: (workerData?: Data) => { + return { + body: { number: factorial(workerData?.body?.number as number) } + } as unknown as Response } }) } } export const requestHandlerWorker = new RequestHandlerWorker< -WorkerData, -WorkerResponse +WorkerData, +WorkerResponse >()