feat: add multithreaded factorial endpoint to express example
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 11 Aug 2023 17:35:18 +0000 (19:35 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 11 Aug 2023 17:35:18 +0000 (19:35 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
.eslintrc.js
examples/typescript/http-server-pool/express/requests.sh
examples/typescript/http-server-pool/express/src/main.ts
examples/typescript/http-server-pool/express/src/types.ts
examples/typescript/http-server-pool/express/src/worker.ts

index 2b402eacb6f4edea0acbc828ec1bdfcb12735c56..44b9c5d292ec749151288997c2f578797c78564a 100644 (file)
@@ -73,6 +73,7 @@ module.exports = defineConfig({
           'poolifier',
           'poolify',
           'readonly',
+          'req',
           'resize',
           'sinon',
           'threadjs',
index e5ad31b3d3358d898e4e65e955f11bf417f64d42..6f7fe0ea19caca7f3432299f93c08320b72f56f9 100755 (executable)
@@ -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
index 02e139399259b4272b958ba41bb9420aef796f3c..b7a713e03ae4798f5aa48b9bf545ab258c49d6e2 100644 (file)
@@ -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(
index 585e87234a93ca8fa4c615433c8837657c53bc29..3bd292153ff3c69e7fbfe133b125459db2e1d0eb 100644 (file)
@@ -1,3 +1,7 @@
+export interface BodyPayload {
+  number?: number
+}
+
 export interface WorkerData<T = unknown> {
   body: T
 }
index f006ad48217ba2a935498a9468a43a4ab5341bf4..7589aa6b6aaa9b37aa519d24c86bafc96d657b98 100644 (file)
@@ -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<BodyPayload>,
+  Response extends WorkerResponse<BodyPayload>
 > extends ThreadWorker<Data, Response> {
   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<BodyPayload>,
+WorkerResponse<BodyPayload>
 >()